Home>

The http request methods specified by the http/1.1 protocol include options, get, head, post, put, delete, trace, and connect. Among them, post is generally used to submit data to the server,This article mainly discusses several ways of submitting data by post.

we know,The http protocol is an application layer specification based on the TCP/IP protocol, which is transmitted in ascii code.The specification divides the http request into three parts:status line, request header, and message body.Similar to the following form:

<method><request-url><version>
<headers>
<entity-body>

The agreement states thatThe data submitted by the post must be placed in the entity-body, but the protocol does not specify what encoding must be used for the data.In fact, developers can completely decide the format of the message body.As long as the last http request meets the above format.

But the data is sent out,It only makes sense for the server to parse successfully.General server-side languages ​​such as php, python, java, .net, etc., 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 headerThen analyze the body.That is, content-type specifies the encoding in the message body. So the post submits the data plan,It is directly related to the content-type and message body.

application/x-www-form-urlencoded

This is the most common way to post data.The browser's native form form, if you do not set the enctype attribute, the data will eventually be submitted as application/x-www-form-urlencoded (the default way for enctype post). The request looks like the following (irrelevant request headers are omitted in this article):

post 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, the content-type is specified as application/x-www-form-urlencoded;

Second, the submitted data is encoded in the form of key1=val1&key2=val2,Both key and val are URL-transcoded. Most server-side languages ​​have good support for this approach.For example, in php, $_post ["title"] can get the value of title, and $_post ["sub"] can get the sub array.

most of the time,When we submit data with ajax,Also use this method.For example, for ajax for jquery and qwrap, the default content-type is "application/x-www-form-urlencoded;charset=utf-8".

multipart/form-data

This post method is also very common.When we upload files using a form,The enctyped of the form must be equal to this value.Here is an example:

post 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 complicated.First, a boundary is generated to separate different fields.To avoid duplication with body content,Boundaries are long and complicated.Then, the content-type indicates what the data is encoded in mutipart/form-data, and what the boundary of this request is.According to the number of fields, the message body is divided into multiple parts with similar structure.Each part starts with --boundary, followed by the content description,Then enterFinally, the specific content of the field (text or binary). If the file is being transferred,Also include file name and file type information.The message body ends with --boundary--.For a detailed definition of mutipart/form-data,Check it out at rfc1867.

This method is generally used to upload files,The major server languages ​​also have good support for it.

The above two post data methods,Are natively supported by the browser,And at this stage, the native form supports only these two methods.But as more and more web sites, especially webapps, all use ajax for data interaction,We can completely define new data submission methods,Bring more convenience to development.

application/json

The application/json content-type is definitely no stranger to us as a response header.Now more and more people use it as a request header,Used 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 lower version ie natively support json.stringify, and server-side languages ​​also have functions to handle json. There is no trouble using json.

The json format supports structured data that is much more complex than key-value pairs,This is useful.Remember, when I was working on a project a few years ago,The level of data to be submitted is very deep,I submitted it after serializing the json data.But at the time, I used the json string as val and still put it in the key-value pair.Submitted as x-www-form-urlencoded.

The ajax function in Google's angularjs defaults to submitting a json string. For example, the following code:

var data={"title":"test", "sub":[1,2,3]};
$http.post (url, data) .success (function (result) {
...
});

The final request sent was:

post http/1.1

content-type:application/json;charset=utf-8

{"title":"test", "sub":[1,2,3]}

This scheme,Can easily submit complex structured data,Especially suitable for restful interfaces. Major package capture tools such as chrome's built-in developer tools, firebug, and fiddler will display json data in a tree structure, which is very friendly.However, some server languages ​​do not support this method yet.For example, php cannot get content from the above request via the $_post object.At this time, you need to do it yourself:When the content-type in the request header is application/json, get the original input stream from php://input,Then json_decode into an object. Some php frameworks are already doing this.

Of course, angularjs can also be configured to submit data using x-www-form-urlencoded.

text/xml

xml-rpc (xml remote procedure call is a protocol that uses http as the transport protocol,xml as a remote calling specification for encoding.A typical xml-rpc request looks like this:

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>

The xml-rpc protocol is simple and functional.There are implementations in various languages.It is also widely used,Such as wordpress's xml-rpc api, search engine's ping service and so on.In JavaScript, there are ready-made libraries that support data interaction in this way,Can well support the existing xml-rpc service. However, I personally feel that the xml structure is still too bloated,General scenarios using json will be more flexible and convenient.

The above are the four common post submission methods that we share with you.Hope you like it.

php
  • Previous Use show () and hide () method to animate show and hide pictures in Jquery
  • Next jQuery controls the DIV layer to achieve the effect of changing from large to small and from far to near animation