No picture
 No picture
 No picture
 No picture
 No picture

Four post modes of Http

Programmer Xiao Yang's code life 2018-04-11
one thousand seven hundred and eighty-nine

1. Introduction

HTTP request methods specified in the HTTP/1.1 protocol include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT. POST is generally used to submit data to the server. This article mainly discusses several ways to submit data through POST. As we know, HTTP protocol is an application layer specification based on TCP/IP protocol and transmitted in ASCII code. The specification divides the HTTP request into three parts: status line, request header, and message body. It is similar to the following:

  1. <method> <request-URL> <version>

  2. <headers>

  3. <entity-body>

However, it is meaningful only when the server successfully parses the data. General server languages such as php and python, as well as their frameworks, have built-in functions to automatically parse common data formats. The server usually knows how the message body in the request is encoded according to the Content Type field in the request headers, and then parses the body. So when it comes to the POST submission data scheme, it includes two parts: Content Type and message body encoding method. The protocol stipulates that the data submitted by POST must be placed in the message body, but the protocol does not specify what encoding method the data must use. In fact, developers can decide the format of the message body by themselves, as long as the final HTTP request meets the above format.

2. Common http post methods:

2.1 application/x-www-form-urlencoded

This should be the most common way to submit data through POST. The browser's native form, if the enctype attribute is not set, will eventually submit data in the application/x-www-form-urlencoded mode. The request is similar to the following (irrelevant request headers are omitted in this article):

  1. POST http : //www.example.com HTTP/1.1

  2. Content - Type : application / x - www - form - urlencoded ; charset = utf - eight

  3. title = test & sub % 5B % 5D = one & sub % 5B % 5D = two & sub % 5B % 5D = three

Many times, when we submit data with Ajax, we also use this method. For example, the Ajax and Content Type default values of JQuery and QWrap are "application/x-www-form-urlencoded; charset=utf-8". First, Content Type is specified as application/x-www-form-urlencoded; Secondly, the submitted data is encoded in the way key1=val1&key2=val2. Both key and val are URL transcoded. Most server languages support this approach well. For example, in PHP$ POST ['title '] can get the value of title$ POST ['sub '] can get the sub array.

2.2 multipart/form-data

This is another common way to submit POST data. When we use forms to upload files, we must make the enctyped of the form equal to this value. Let's take a look at a request example:

  1. POST http : //www.example.com HTTP/1.1

  2. Content - Type : multipart / form - data ; boundary =---- WebKitFormBoundaryrGKCBY7qhFd3TrwA

  3. ------ WebKitFormBoundaryrGKCBY7qhFd3TrwA

  4. Content - Disposition : form - data ; name = "text"

  5. title

  6. ------ WebKitFormBoundaryrGKCBY7qhFd3TrwA

  7. Content - Disposition : form - data ; name = "file" ; filename = "chrome.png"

  8. Content - Type : image / png

  9. PNG ... content of chrome . png ...

  10. ------ WebKitFormBoundaryrGKCBY7qhFd3TrwA --

This method is generally used to upload files. It is also well supported by major server languages. This example is a little more complicated. First, a boundary is generated to split different fields. To avoid duplication with the body content, the boundary is very long and complex. Then the Content Type indicates that the data is encoded in multipart/form data, and what the boundary of this request is. The message body is divided into multiple parts with similar structure according to the number of fields. Each part starts with the - - boundary, followed by the content description information, followed by the carriage return, and finally the specific content of the field (text or binary). If you are transferring a file, you should also include the file name and file type information. The end of the message body is marked with - - background - -. For detailed definitions of multipart/form data, please go to rfc1867. The two POST data modes mentioned above are both natively supported by the browser, and only these two modes are supported by the native form at this stage. But as more and more Web sites, especially WebApps, use Ajax for data interaction, we can completely define new data submission methods, bringing more convenience to development.

2.3 application/json

As the response header, the Content Type of application/json must be familiar to everyone. In fact, more and more people use it as a request header to tell the server that the message body is a serialized JSON string. Due to the popularity of the JSON specification, all major browsers except the low version IE natively support JSON.stringify, and the server-side languages also have JSON processing functions, so you won't have any trouble using JSON. It is also useful that the JSON format supports structured data that is much more complex than key value pairs. The Ajax function in Google's AngularJS is to submit JSON strings by default. For example, the following code:

  1. var data = { 'title' : 'test' , 'sub' : [ one , two , three ]};

  2. $http . post ( url , data ). success ( function ( result ) {

  3.     ...

  4. });

The final request sent is:

  1. POST http : //www.example.com HTTP/1.1

  2. Content - Type : application / json ; charset = utf - eight

  3. { "title" : "test" , "sub" :[ one , two , three ]}

Of course, AngularJS can also be configured to submit data in x-www-form-urlencoded mode. Please refer to this article if necessary. This solution can easily submit complex structured data, and is particularly suitable for RESTful interfaces. All major packet capturing tools, such as Chrome's own developer tools, Firebug and Fiddler, display JSON data in a tree structure, which is very friendly. However, some server languages do not support this method. For example, PHP cannot pass$ The POST object gets the content from the above request. At this time, you need to do it yourself: when the Content Type in the request header is application/json php://input Get the original input stream from the Decode into objects. Some php frameworks are already doing this.

2.4 text/xml

XML-RPC(XML Remote Procedure Call)。 It is a remote call specification that uses HTTP as the transmission protocol and XML as the encoding method. A typical XML-RPC request is as follows:

  1. POST http : //www.example.com HTTP/1.1

  2. Content - Type : text / xml

  3. <? xml version = "1.0" ?>

  4. <methodCall>

  5.     <methodName> examples . getStateName </ methodName >

  6.     <params>

  7.         <param>

  8.             <value><i4> forty-one < /i4></ value >

  9.         </ param >

  10.     </ params >

  11. </ methodCall >

XML-RPC protocol is simple and functional, and can be implemented in various languages. It is also widely used, such as the XML RPC Api of WordPress, the ping service of search engines, and so on. In JavaScript, there are also ready-made libraries 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, and JSON is more flexible and convenient in general scenarios

Reference article: https://imququ.com/post/four-ways-to-post-data-in-http.html


Welcome to follow my public account, get more articles, and communicate with me.


The article was reprinted from Programmer xiao yang's code life , if suspected of infringement, please send an email to: contact@modb.pro Make a report and provide relevant evidence. Once verified, Mo Tianlun will delete relevant content immediately.

comment