Three encoding formats for submitting data in POST method

The HTTP request methods specified in HTTP/1.1 protocol include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE and CONNECT. The POST method is generally used to submit data to the server, and I am going to mainly discusses several encoding methods of POST submitted data.

The HTTP protocol specifies that the data submitted by POST method must be placed in the entity-body of the message, but it does not specify how the data can be encoded. However, if the data is sent, it makes sense for the server to parse it successfully.

For the server who receives the data, How does it know what encoding format is used for the transmitted data? The answer is Content-Type field in the request header. Here are 3 common value of Content-Type.

application/x-www-form-urlencodedThe default submission method for a form tag in HTML
multipart/form-dataUsed when uploading files
text/xml, application/json, text/plainNormal text

1. application/x-www-form-urlencoded

This should be the most common way to POST data. If the enctype property is not set in the browser’s native form, the data will eventually be submitted in application/x-www-form-urlencoded mode.

The transported data that content-type is specified as application/x-www-form-urlencoded is encoded as key1=val1&key2=val2, with URL transcoding for key and val. Most back-end languages have good support for this approach.

2. multipart/form-data

This is also a common way to submit POST data. When we need to upload a file using a form, we must set the enctype property of the form to multipart/form-data. This method is generally used to transport binary stream data, and it is also well-supported by various back-end languages. Both of the above mentioned formats of data are supported natively by the today’s browser.

3. text/xml, application/json, text/plain

This three encoding format is all used to send data as normal text. For now, more and more people are using application/json as a request header to notify the server that the body of the message is a serialized JSON string. Due to the popularity of the JSON specification, all major browsers support jSON.Stringify method natively except the old version of IE.

There is no good or bad way to encode data, but the most important thing is to use the right format in the right scenario.

RSS