It is very important for any developer to know how the POST and GET parameters are sent over the network. It's a core functionality of any web application. You may use these parameters in almost any project. As a result, this guide may help you to learn what's the difference between POST and GET values. Also, you will see here how they are stored in the request body.
The POST request may be used to send data to the server when you are updating forms, creating articles, submitting comments and so on. While the GET request is usually used to simply obtain the data from the remote host. In both those cases, you may specify additional parameters. See in the following sections how exactly such behavior is implemented in the HTTP protocol.
How the GET parameters are sent to the server
The GET parameters are usually added to the request URL in the form of a query. There should be the ? symbol to separate the location of the document from the query parameters. In the following sample you can see an example of the simple GET request that has no additional parameters.
GET /document.html
But, if you do want to use the query string, then the request may look like the following:
GET /document.html?order=asc
In that case, the name of the parameter is order. Then we use the = symbol to provide a value, which is "asc" in our sample. Also, you may provide several parameters in a single query. See how it can be done below.
GET /document.html?filter=new&order=asc&page=2
As you may notice, the groups of values are separated by the & symbol. It allows you to pass multiple values in a single query string.
The query string may also end with the # symbol. It allows you to specify a fragment that identifies a specific element on a target resource. Below is our example of such a query.
GET /document.html?filter=new&order=asc&page=2#heading
If your document has a lot of query parameters, it is worth using the canonical meta tag in the HTML web page. Otherwise, the search engines may consider that the site has duplicated content. And it may have a negative effect on the site ranking.
The canonical meta tag should have a full URL of a page. There should be no additional query parameters. Then, the search engines will combine all similar pages into a single resource.
Finally, it is worth noting that it's not the best option to send the data in a body section of the GET request. It may work in some cases. But there is a risk that such a request may fail. If you do need to send some values in the body of the HTTP request, then you may consider using the POST request. It's described in the following section.
How the POST parameters are sent to the server
The POST request is usually used to send some data using a HTML form. It should have a body section that contains the given parameters. There must be additional configuration that specifies which content is transferred over the network. This means that the server should process the request according to the given options.
Also, it's better to not use any sort of a cache for the server response. The client should be able to get valid and fresh data, since the POST request may produce "side effects".
Finally, the server may send various status codes, such as 200, 201 or 204. It is worth writing a code that will appropriately process each response type.
Content types
If you are making a form in a HTML web page, then you may need to configure the "enctype" property. It should be used to specify the appropriate content type.
Following is the list of possible values that you may assign for that property:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
Below is an example of how to configure the form in a HTML code.
<form
action="https://example.com/document"
enctype="multipart/form-data"
method="post"
>
<p>First name <input type="text" name="first-name"></p>
<p>Last name <input type="text" name="last-name"></p>
<p><input type="submit" value="Send"></p>
</form>
application/x-www-form-urlencoded
This content type should be used for sending keys and values to the server. There must be no binary data in the HTTP request. The format of the query is very similar to the query that is used in the GET request.
The string must be a collection of the key and value pairs that are connected by the & symbol. Each item starts with the key name. Then, we have to put the = symbol. And after that, we have to provide a specific value. The query must be an URL that is encoded according to the applicable RFC specification.
Below, you can see a very simple example of the POST request that uses the HTTP/1.1 protocol to send a form data.
POST /document HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 20
filter=new&order=asc
multipart/form-data
This type should be used for sending file and binary data from client to the server. However, to allow that, some additional options must be added to the request.
For example, you have to configure the Content-Type header in a special way. There should be the boundary option. It defines a special delimiter that is used as separate content in a HTTP body. See the example below.
Content-Type: multipart/form-data; boundary="separator"
Now, you can create a header that uses the given specification. The full request may look like it's shown below.
POST /test HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary="separator"
--separator
Content-Disposition: form-data; name="submit-name"
Example
--separator
Content-Disposition: form-data; name="files"; filename="data.txt"
Content-Type: text/plain
... data.txt ...
--separator--
text/plain
That's a very simple content type. It specifies data that should be understandable by a user. There must not be binary data or special characters in a content of such type.
In other words, if you specify such a type, it means that you have to provide a regular plain text in the HTTP request.
When to use the GET request
The GET request is very simple. It should be used only for obtaining the data from the remote location. There is no need to include the data in the request body.
However, some additional information may be added directly to the URL query. For example, you can include the order or page parameters to adjust the data that is returned to the client.
Usually, the GET request is used to get the content of a web page. But you may also use this type of request in your custom web applications. There can be a REST API that returns information about entries stored in the database. Or you may use the GET query to get other types of remote data.
But, the other important note is that the GET responses may be cached. Multiple requests to the same endpoint may get the data that was generated for the first request. This can improve the performance of your application. However, it's for you to decide what type of cache to use, since it totally depends on the technology stack that is used in your project.
When to use the POST request
A POST request is very common in web development. It can be used for transferring various types of information from the client to the server. Contact form submissions, comments, newsletters, mailing lists and other types forms can be created with help of this request.
It is worth mentioning that web applications may perform two actions, creating a form and updating a form. The POST request can work in both those cases. However, in case of updating the data, you may need to provide some additional POST field that contains the ID of the entry to update in the database.
Please pay attention that sending data to the server is a risky task. You may also need to add additional security layers to verify that the data is correct. Also, it's important to sanitize the form data before saving it to the database.
Also, it's for you to decide whether to use this method or not. The general advice is to follow the common recommendations on how to build a REST API.
Mixed request to the server
You may also try to use both GET and POST parameters in a single request to the server. That is possible from the technical point of view. That is a regular POST request, but it also contains a query string in the URL.
This method may work in some cases. However, it may be confusing for some developers. It's easy to make a mistake when trying to read the parameters on the server side. As a result, it makes sense to use only a single convention for the HTTP communication between client and server.
Conclusion
As you see, this topic is very complicated. The GET and POST parameters may be used in very different ways. The HTTP protocol is flexible. The developer that makes the application should choose the solution that is the most efficient to perform the required job.
However, the best advice is to follow the common principles of a web application design. We recommend you to review the relevant RFC specifications to get a deeper knowledge of how the HTTP protocol works. There may be some other specific cases that are better described in the relevant guides.
Comments