Home >Backend Development >PHP Tutorial >Four common POST data submission methods (short summary), four post_PHP tutorials

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

WBOY
WBOYOriginal
2016-07-12 09:07:58768browse

Four common POST data submission methods (small summary), four kinds of post

The HTTP request methods specified by the HTTP/1.1 protocol include OPTIONS, GET, HEAD, POST, and PUT , DELETE, TRACE, CONNECT. Among them, POST is generally used to submit data to the server. This article mainly discusses several ways of submitting data through POST.

We know that the HTTP protocol is transmitted in ASCII code and is an application layer specification based on the TCP/IP protocol. The specification divides HTTP requests into three parts: status line, request headers, and message body. Similar to the following form:

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

The protocol stipulates that the data submitted by POST must be placed in the message body (entity-body), but the protocol does not specify what encoding method the data must use. In fact, developers can completely decide the format of the message body by themselves, as long as the last HTTP request sent meets the above format.

However, before the data is sent out, it only makes sense if the server successfully parses it. General server-side languages ​​such as php, python, Java, .NET, etc., and their frameworks have built-in functions for automatically parsing common data formats. The server usually learns how the message body in the request is encoded based on the Content-Type field in the request header, and then parses the body. That is, Content-Type specifies the encoding in the message body. Therefore, the POST submission data scheme is directly related to the Content-Type and the message body.

application/x-www-form-urlencoded

This is the most common way to submit data via POST. The browser's native form form, if the enctype attribute is not set, will eventually submit data in the application/x-www-form-urlencoded method (enctype's POST default method). The request is similar to the following (irrelevant 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[]=1&sub[]=2&sub[]=3

First, the Content-Type is specified as application/x-www-form-urlencoded;
Secondly, the submitted data is encoded according to key1=val1&key2=val2, and both key and val are URL transcoded. Most server-side languages ​​have good support for this method. For example, in PHP, $_POST['title'] can get the value of title, and $_POST['sub'] can get the sub array.

Many times, we also use this method when submitting data using Ajax. For example, the Ajax of Jquery and QWrap, the default value of Content-Type is "application/x-www-form-urlencoded;charset=utf-8".

multipart/form-data

This POST method is also very common. When we use a form to upload files, the enctyped of the form must be equal to this value. Here 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 more complicated. First, a boundary is generated to separate different fields. In order to avoid duplication with the text content, the boundary is very long and complicated. Then, Content-Type specifies that the data is encoded with mutipart/form-data and what is the boundary of this request. The message body is divided into multiple parts with similar structure according to the number of fields. Each part starts with --boundary, followed by content description information, followed by carriage return, and finally the specific content of the field (text or binary). If a file is being transferred, the file name and file type information must also be included. The message body ends with the --boundary-- flag. For a detailed definition of mutipart/form-data, please go to rfc1867.

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

The above two POST data methods are natively supported by browsers, and at this stage, native forms only support 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 to bring more convenience to development.

application/json

The Content-Type of application/json is certainly familiar to everyone as a response header. Now 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 lower versions of IE natively support JSON.stringify. Server-side languages ​​also have functions for processing JSON, so you will not encounter any trouble when using JSON.

JSON 格式支持比键值对复杂得多的结构化数据,这一点很有用。记得,我几年前做一个项目时,需要提交的数据层次非常深,我就是把数据 JSON 序列化之后来提交的。不过当时我是把 JSON 字符串作为 val,仍然放在键值对里,以 x-www-form-urlencoded 方式提交。

Google 的 AngularJS 中的 Ajax 功能,默认就是提交 JSON 字符串。例如下面代码:

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

最终发送的请求是:

POST http://www.example.com HTTP/1.1
Content-Type: application/json;charset=utf-8
{"title":"test","sub":[1,2,3]}

这种方案,可以方便的提交复杂的结构化数据,特别适合 RESTful 的接口。各大抓包工具如 Chrome 自带的开发者工具、Firebug、Fiddler,都会以树形结构展示 JSON 数据,非常友好。但也有些服务端语言还没有支持这种方式,例如,php 就无法通过 $_POST 对象从上面的请求中获得内容。这时候,需要自己动手处理下:在请求头中 Content-Type 为 application/json 时,从 php://input 里获得原始输入流,再 json_decode 成对象。一些 php 框架已经开始这么做了。

当然 AngularJS 也可以配置为使用 x-www-form-urlencoded 方式提交数据。

text/xml

XML-RPC(XML Remote Procedure Call 是一种使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范。典型的 XML-RPC 请求是这样的:

POST http://www.example.com HTTP/1.1
Content-Type: text/xml
<&#63;xml version="1.0"&#63;>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>

XML-RPC 协议简单、功能够用,各种语言的实现都有。它的使用也很广泛,如 WordPress 的 XML-RPC Api,搜索引擎的 ping 服务等等。JavaScript 中,也有现成的库支持以这种方式进行数据交互,能很好的支持已有的 XML-RPC 服务。不过,我个人觉得 XML 结构还是过于臃肿,一般场景用 JSON 会更灵活方便。

以上内容是小编跟大家分享的常见的四种POST 提交数据方式,希望大家喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1058144.htmlTechArticle常见的四种POST 提交数据方式(小总结),四种post HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 这几种。...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn