The HTTP request method specified in the HTTP/1.1 protocol is Options, GET, Head, Post, Put, Delete, Trace, Connect. Among them, POST is generally used to submit data to the server. This article mainly discusses several methods for POST submitted data.
We know that the HTTP protocol is transmitted by ASCII code, and the application layer specifications based on the TCP/IP protocol. The specifications divide HTTP requests into three parts: state line, request head, and message subject. Similar to the following:
<emod> <repkest-url> <vision> <headers> <Entity-Body> </Entity-Body> </Headers> </Version> </Request-Url> </Method> </Method> </Method> </Method>
The agreement stipulates that the data submitted by the POST must be placed in the Entity-Body, but the agreement does not specify what the data must be used. In fact, developers can determine the format of the message subject by themselves.
However, if the data is sent out, it is meaningful to be successful in the server. The general server language such as Java and their Framework have built -in functions of automatic analysis of common data formats. The server is usually learned from the Content-Type field in the request head to learn how the message subject in the request is encoded in the request, and then analyzes the subject. So when it comes to POST submitted data schemes, it contains two parts: Content-Type and message subject coding. Here are official introduction to them.
Application/x-WWW-Form-UrlencodedThis should be the most common way to submit data. If the native Form forms of the browser, if the Enctype attribute is not set, then the data will be submitted by Application/X-WWW-Form-UrlenCoded. The request is similar to the following (the irrelevant request header is omitted in this article):
Post http://www.example.com http/1.1Content-Type: Application/X-WWW-FORLEM-Urlencoded; Charset = UTF-8Tital = Test & Sub%5D = 1 & SUB%5D 5D = 2 & sub%5b%5D = 3
First of all, Content-Type is specified as Application/X-WWW-Form-Urlencoded; second, the submitted data is encoded according to the key1 = value & key2 = value, and both and Val perform URL transcoding. Most server languages have good support for this method. For example, in PHP, post [′ title ′] can get the value of the title. Post [′ Title ′] can get the value of the tity.
Many times, when we submit data with AJAX, we also use this method. For example, jQuery's ajax, Content-Type's default value is "Application/X-WWW-Form-Urlencoded; Charset = UTF-8".
Multipart/Form-DataThis is another common way of posting data. When we upload files with forms, we must make Form's Enctyped equal to this value. Come directly to a request example:
Post http://www.example.com http/1.1Content-Type: Multipart/Form-Data; Boundary = ---- WebkitForeMboundRGKCBY7QHFD3TRWA ----- WEBKITFORMBOONN DaryRGKCBY7QHFD3TRWACONTENT-DISPOSITION: Form-Data; name = TextTitle --- ---WebKitFormBoundaryrGKCBY7qhFd3TrwAContent-Disposition: form-data; name=file; filename=chrome.pngContent-Type: image/pngPNG ... content of chrome.png ...------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
This example is slightly complicated. First of all, a Boundary is used to divide different fields. In order to avoid repeating with the text, Boundary is very long and complicated. Then Content-Type indicates that the data is encoded by Mutipart/Form-Data, what is the Boundary requested this time. The message subject is divided into similar parts with a similar structure according to the number of sections. Each part starts with --boundary, followed by the content description information, and then Enter. Finally, the specific content of the field (text or binary). If the file is transmitted, it also contains file name and file type information. The message subject finally ended with --boundary-. For detailed definitions of Mutipart/Form-Data, please go to RFC1867 to view.
This method is generally used to upload files, and the major server language also has a good support for it.
The two post data methods mentioned above are native support for browsers, and the native Form forms only support these two methods at this stage. However, as more and more web sites, especially WebApp, all use AJAX for data interaction, we can completely define new data submission methods to bring more convenience to development.
application/jsonApplication/JSON, the Content-Type, is definitely not unfamiliar with the response header. In fact, more and more people now use it as a request head to tell the server message that the main body is a serialized JSON string. Due to the popularity of JSON specifications, the major browsers except the low -version IE are native to JSON.Stringify, and the server language also has a function to deal with JSON, and JSON will not encounter any trouble.
The JSON format supports more complicated structured data than key values, which is also useful. I remember when I did a project a few years ago, the data level I needed to be submitted was very deep. I submitted the data JSON serialized. However, at the time, I used the JSON string as a value, and I was still put in the key value pair, and submitted it in X-WWW-Form-Urlencoded.
The AJAX function in AngularJS of Google is to submit the JSON string by default. For example, the following code:
var data = {'title': 'test', 'sub': [1,2,3]}; $ http.post (url, data) .success (function (result) {...});
The final request is:
Post http://www.example.com http/1.1content-type: Application/json; char set = UTF-8 {Title: test, sub: [1,2,3]}}
This scheme can easily submit complex structured data, especially suitable for RESTFUL interface. Major packaging tools such as Chrome's own developer tools, Firebug, and Fiddler will display JSON data with a tree structure, which is very friendly. However, there are also some server -side languages that have not supported this method. For example, PHP cannot obtain content from the above request through the $ _post object. At this time, you need to handle it by yourself: When Content-Type is application/json in the request header, obtain the original input stream from PHP: // input, and then json_decode. Some Java frameworks have begun to do.
Of course, AngularJS can also be configured to submit data with X-WWW-Form-UrlenCoded.
Text/XMLXML-RPC (XML Remote Procedure Call). It is a remote call specification using HTTP as a transmission protocol and XML as a encoding method. The typical xml-rpc request is this: post http://www.example.com http/1.1Content-Type: Text/XML <!-? Xml Version = 1.0 ?-> <MthodCall> <MthodName> Examples .getStatename </MethodName> <Params> <Param> <Value> <i4> 41 </i4> </Value> </Params> </MethodCall>
The XML-RPC protocol is simple and full of functions, and the implementation of various languages is available. It is also widely used, such as the XML-RPC API of WordPress, the ping service of the search engine, and so on. In JavaScript, there is also a ready-made library to support data interaction in this way, which can well support existing XML-RPC services. However, I personally think that the XML structure is still too bloated. Generally, JSON will be more flexible and convenient.