Four common POST data submission methods (summary) and four post_PHP tutorials

Source: Internet
Author: User
There are four common POST data submission methods (summary) and four post methods. Four common POST data submission methods (summary ), the four postHTTP1.1 protocols stipulate the following HTTP request methods: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT. Four common POST data submission methods (summary) and four post

The HTTP/1.1 protocol specifies the following HTTP request methods: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT. Here, POST is generally used to submit data to the server. This article mainly discusses several methods for submitting data through POST.

We know that the HTTP protocol is based on ASCII code transmission and is based on the application layer specification built on the TCP/IP protocol. HTTP requests are divided into three parts: status line, request header, and message body. Similar to the following format:

  
   
   
    
     
    
   
  
 

The protocol stipulates that the data submitted by POST must be placed in the message body, but the protocol does not specify the encoding method required for the data. In fact, developers can determine the format of the message body by themselves, as long as the final HTTP request meets the above format.

However, if the data is sent out, it must be successfully parsed by the server. Generally, server-side languages such as php, python, Java, and. NET, and their frameworks all have built-in functions for automatic parsing of common data formats. The server usually obtains the encoding method of the message body in the request based on the Content-Type field in the request header (headers), and then parses the body. That is, Content-Type specifies the encoding method in the message body. Therefore, the POST data submission scheme is directly related to the Content-Type and message body.

Application/x-www-form-urlencoded

This is the most common way to submit data through POST. If the native form of the browser does not set the enctype attribute, data will be submitted in application/x-www-form-urlencoded mode (the default POST mode of enctype ). The request is similar to the following (unrelated request headers are omitted in this article ):

POST http://www.example.com 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, Content-Type is specified as application/x-www-form-urlencoded;
Second, the submitted data is encoded by key1 = val1 & key2 = val2, and both key and val are URL transcoded. Most server languages support this method. For example, in PHP, $ _ POST ['title'] can get the title value, and $ _ POST ['sub'] can get the sub array.

This method is often used when Ajax is used to submit data. For example, for Ajax and Content-Type of Jquery and QWrap, the default values are "application/x-www-form-urlencoded; charset = utf-8 」.

Multipart/form-data

This POST method is also common. When we use a form to upload a file, we must make the form's enctyped equal to this value. The following is an example:

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 a little complicated. First, a boundary is generated to separate different fields. to avoid duplication with the body content, boundary is very long and complex. Then, Content-Type indicates that the data is encoded by mutipart/form-data. what is the boundary of this request. The message body is divided into similar parts according to the number of fields. each part starts with -- boundary, followed by the content description, followed by the carriage return, finally, the specific content of the field (text or binary ). If a file is transmitted, it also contains the file name and file type information. The message subject ends with the -- boundary -- mark. For detailed definitions of mutipart/form-data, go to rfc1867.

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

The above two POST data methods are supported by the browser native, and currently the native form only supports these two methods. However, as more and more Web sites, especially webapps, use Ajax for data interaction, we can define new data submission methods to facilitate development.

Application/json

The Content-Type of application/json is the response header. More and more people now use it as the request header to tell the server that the message body is a serialized JSON string. Due to the popularity of JSON specifications, all major browsers except earlier versions of IE support JSON. stringify native, and the server language also has functions for processing JSON, so there is no trouble in using JSON.

JSON format supports more complex structured data than key-value pairs, which is useful. I remember that when I was working on a project a few years ago, I had to submit a very deep data hierarchy. I just submitted the data after JSON serialization. However, at that time, I used the JSON string as the val and put it in the key-value pair and submitted it in x-www-form-urlencoded mode.

The Ajax function in AngularJS of Google submits JSON strings 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.1
Content-Type: application/json; charset = utf-8
{"Title": "test", "sub": [1, 2, 3]}

This solution can easily submit complex structured data and is especially suitable for RESTful interfaces. All major packet capture tools, such as Chrome's 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. 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 the Content-Type in the request header is application/json, get the original input stream from php: // input, and then json_decode the object. Some php frameworks have already started to do this.

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

Text/xml

XML-RPC (XML Remote Procedure Call is a Remote Call specification that uses HTTP as the transport protocol and XML as the encoding method. A typical XML-RPC request is like this:

POST http://www.example.com HTTP/1.1Content-Type: text/xml<?xml version="1.0"?>
 
  
   examples.getStateName
  
  
   
   
    
     41
    
   
  
 

XML-RPC protocol is simple, the function is enough, the realization of various languages have. It is also widely used, such as WordPress XML-RPC Api, search engine ping service and so on. In JavaScript, there are also ready-made libraries to support data interaction in this way, can well support the existing XML-RPC services. However, I personally think the XML structure is too bloated, and JSON is more flexible and convenient in general scenarios.

The above content is a common four POST data submission method shared by Xiaobian. I hope you will like it.

Shard data submission method (summary ), the four types of HTTP request methods stipulated by the post HTTP/1.1 protocol include OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, and CONNECT ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.