HTTP POST Method

The HTTP POST method is used to send data to a server to create/update a resource

The HTTP POST method is one of the most widely used HTTP methods along with GET and HEAD. The data sent to the server with POST methods are stored in the request body of the HTTP request.

Example of HTTP POST command:
POST /wiki/Main_Page HTTP/1.1
Host: en.wikipedia.org
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate


POST Headers

HTTP POST Request Headers in HTTP Debugger

Server Response Headers

Server Response Headers in HTTP Debugger

GET vs POST

GET POST
Browser BACK button/Reload Harmless Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
History Parameters remain in browser history Parameters are not saved in browser history
Restrictions on data length Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) No restrictions
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also allowed
Security GET is less secure compared to POST because data sent is part of the URL

Never use GET when sending passwords or other sensitive information!
Visibility Data is visible to everyone in the URL Data is not displayed in the URL

PUT vs POST

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. In contrast; the URI in a PUT request identifies the entity enclosed with the request. Practically speaking, POST is used to append a resource to an existing collection.

Using the HTTP POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI.

The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, or a record is subordinate to a database.

The POST allows posting messages to bulletin boards, mailing lists and newsgroups. The HTTP POST is used for providing data blocks to the server.

For example, it can be used for submitting completed form details. The HTTP POST is also used for data-handling; appending new entities to a database is usually done with HTTP POST.

It is recommended to use HTTP POST if there is a database modification. The HTTP POST responses are not cacheable unless the HTTP POST response contains the appropriate header.

However, the HTTP POST response is used to transfer cacheable resources.
The web server defines an actual function performed while receiving the HTTP POST request from a web browser and the HTTP POST usually depends on the request-URI.

Sending Custom HTTP POST Requests

Sending Custom HTTP POST Requests with HTTP Debugger

How does HTTP POST work

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

The POST method does not have any restriction on data size to be sent and can be used to send ASCII as well as binary data.

The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

The PHP provides $_POST associative array to access all the sent information using POST method. Try out following example by putting the source code in test.php script.

<?php
   if( $_POST["name"] || $_POST["age"] ) {
      if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
         die ("invalid name and name should be alpha");
      }
      echo "Welcome ". $_POST['name']. "<br />";
      echo "You are ". $_POST['age']. " years old.";
      
      exit();
   }
?>
<html>
<body>  
<form action = "<?php $_PHP_SELF ?>" method = "POST">
         Name: <input type = "text" name = "name" />
         Age: <input type = "text" name = "age" />
         <input type = "submit" />
</form>   
</body>
</html>


Copyright Notice: Please don't copy or translate this article without prior written permission from the HTTPDebugger.com

HTTP Debugger is a proxy-less HTTP analyzer for developers that provides the ability to capture and analyze HTTP headers, cookies, POST params, HTTP content and CORS headers from any browser or desktop application. Awesome UI and very easy to use. Not a proxy, no network issues!
Download FREE 7-Day Trial

HTTP Debugger

HTTP Debugger - Professional HTTP Analyzer Debug HTTP API calls to a back-end and between back-ends. Very easy to use. Not a proxy, no network issues! Download Free Trial