Four encoding methods for HTTP Post requests

The HTTP protocol stipulates that the data submitted by POST must be placed in the message body, but the protocol does not stipulate that the data must What encoding method is used In fact, developers can decide the format of the message body by themselves, as long as the final HTTP request meets the above format. However, it is meaningful only when the server successfully parses the data. Common server languages such as php Python and their frameworks have built-in functions to automatically parse common data formats.
The server is usually based on the Content-Type Field to know how the message body in the request is encoded, and then parse the body.
So when it comes to the POST submission data scheme, it includes two parts: Content Type and message body encoding method.

In the POST request, the submitted data is placed in the message body, and four data encoding methods are provided.

The enctype attribute of the FORM form specifies how the form data should be encoded before being sent to the server.
By default, the form data will be encoded as "application/x-www-form-urlencoded". That is, before sending to the server, all characters will be encoded (spaces are converted to "+" plus signs, and special symbols are converted to ASCII HEX values).

0. Four enctype attribute values

value describe
application/x-www-form-urlencoded Encode all characters before sending (default)
multipart/form-data Don't encode characters. This value must be used when using a form that contains a file upload control.
application/json
text/plain Spaces are converted to "+" plus signs, but special characters are not encoded.


Now let's officially introduce them.

1.application/x-www-form-urlencoded

This should be the most common way to submit data through POST. The browser's native<form>form, if not set enctype Property, the data will be submitted in the application/x-www-form-urlencoded mode.

<form action="form_action.asp" enctype="text/plain">
<p>First name: <input type="text" name="fname" /></p>
<p>Last name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>

At this time, the request data submitted by the form and the request seen during packet capturing will be like this (irrelevant request headers are omitted in this article):

POST http://www.example.com HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=utf-8
title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

first, Content Type is specified as application/x-www-form-urlencoded;

Secondly, the data submitted shall be in accordance with Key1=val1&key2=val2 Both key and val have URL transcoding.
Most server languages support this approach well. For example, in PHP, $_POST ['title '] can get the value of title, and $_POST ['sub'] can get the sub array.

Many times, when we submit data with Ajax, we also use this method.
for example JQuery and QWrap Ajax, Content-Type Default All are "application"/ x-www-form-urlencoded ; charset=utf-8」。

2.multipart/form-data

This is another common way to submit POST data. We use forms Upload file You must make the enctype of the<form>form equal to multipart/form data. Let's take a look at a request example:

POST http://www.exam

Issued on 2022-10-26 17:08