Four Ways to Post Data in HTTP
When sending HTTP requests, there are several ways to post data, each with its own advantages and use cases. In this article, we will explore four common methods: application/x-www-form-urlencoded, multipart/form-data, application/json, and text/xml.
1. application/x-www-form-urlencoded
This is the most common way to post data in HTTP. When a <form> element is not explicitly set with an enctype attribute, the browser will submit the data in application/x-www-form-urlencoded format. The data is encoded in the URL, with each key-value pair separated by an ampersand (&).
Here is an example of a request:
POST http://www.example.com HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8
title=test&sub[0]=1&sub[1]=2&sub[2]=3
Most server-side languages, such as PHP, have good support for this approach. For example, in PHP, you can access the title value using $ _POST['title'], and the sub value as an array using $ _POST['sub'].
2. multipart/form-data
This method is commonly used to upload files. When a <form> element is set with an enctype attribute of multipart/form-data, the browser will submit the data in this format.
Here is an example of a request:
POST http://www.example.com HTTP/1.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="text"
title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="file"; filename="chrome.png"
Content-Type: image/png
PNG ... content of chrome.png ...
------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
This example is slightly more complex, as it requires a boundary to be generated to separate the different fields. The Content-Type indicates that the data is multipart/form-data encoded, and the boundary is specified in the request.
3. application/json
This method is becoming increasingly popular, especially for RESTful interfaces. The Content-Type header is set to application/json, and the message body contains a JSON string.
Here is an example of a request:
POST http://www.example.com HTTP/1.1
Content-Type: application/json; charset=utf-8
{ "Title": "test", "sub": [1,2,3] }
This method is convenient for submitting complex data structures, especially for RESTful interfaces. However, some server-side languages, such as PHP, do not natively support this method. In such cases, you need to manually parse the JSON data from the input stream using json_decode.
4. text/xml
This method is commonly used for XML-RPC (XML Remote Procedure Call) requests. The Content-Type header is set to text/xml, and the message body contains an XML string.
Here is an example of a request:
POST http://www.example.com HTTP/1.1
Content-Type: text/xml
<?xml version="1.0"?>
<MethodCall>
<MethodName>examples.getStateName</methodName>
<Params>
<Param>
<Value><i4>41</i4></Value>
</Param>
</Params>
</MethodCall>
XML-RPC is a simple and functional protocol that allows for data exchange between different languages. However, its use is becoming less common, as JSON is more flexible and convenient for most use cases.
In conclusion, there are four common ways to post data in HTTP: application/x-www-form-urlencoded, multipart/form-data, application/json, and text/xml. Each method has its own advantages and use cases, and understanding these methods is essential for any web developer.