How to mimic an HTML form submission in a POST request?

54,067

A POST request consists of a number of headers and a request body. When you submit a form, the browser URL encodes names and values of all form fields and then puts them in the request body in this format:

fieldname1=fieldvalue1&fieldname2=fieldvalue2

I.e. the request body looks like a typical query string.


Here's what the request could look like for your form:

POST /bugreport.php HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: [size of the request body]

logfile=blabla&configfile=more+blabla&usercomment=hello&useremail=

To make sure your program matches what a browser would do, you can post the form with Firefox and then inspect the request headers and body using Firebug's net panel.

Share:
54,067
Violet Giraffe
Author by

Violet Giraffe

I'm from Kharkiv, Ukraine. My city is being destroyed every day. Help us defend: https://www.comebackalive.in.ua/donate PC enthusiast, C++ fan and C++ / Qt / Android software developer. Also <3 Arduino. Music and car addict. Reinventing various wheels in my spare time. A custom-made square wheel is like a tailored suit, wouldn't you agree? Why do I have more rep from questions than from answers? Because you don't learn by answering; you learn by asking questions :) Ongoing personal open-source projects: Dual-panel file manager for Windows / Linux / Mac

Updated on July 26, 2022

Comments

  • Violet Giraffe
    Violet Giraffe almost 2 years

    I have HTML form that is used for sending bugreports from application to server. I need to mimic this behavior programmatically. What will the corresponding POST request (or series of requests) look like?

    <form name="bugreport" method="post" enctype="multipart/form-data" action="http://my-server.com/bugreport.php">
        <div name="SentData">
            <textarea name="logfile" class="UserVisible"></textarea><br>
            <textarea name="configfile" class="UserVisible"></textarea><br>
        </div>
        <textarea name="usercomment" class="invisible"></textarea><br>
        <input name="useremail" type="text" class="invisible">
        <input class="invisible" type="submit" value="Send">
    </form>