Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>  
like image 327
Violet Giraffe Avatar asked Dec 07 '11 14:12

Violet Giraffe


People also ask

Can an HTML form be submitted using GET instead of POST?

As with submitting any form data, you have the option of submitting your data in the form of GET requests, and you will save a few lines of code if you do so. However, there is a downside: some browsers may cache GET requests, whereas POST requests will never be cached.

How can I send form data in POST request?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

What are the two methods of HTML form submission?

The HTML <form> method Attribute is used to specify the HTTP method used to send data while submitting the form. There are two kinds of HTTP methods, which are GET and POST. The method attribute can be used with the <form> element.

How do I send a POST request in HTML?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.


1 Answers

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.

like image 98
Martin Avatar answered Sep 30 '22 13:09

Martin