Four common POST submission data methods (transfer)

https://imququ.com/post/four-ways-to-post-data-in-http.html#simple_thread

Four common POST submission data methods

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 to submit data through POST.

We know that 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: the status line, the request header, and the message body. Something like this:

BASH<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 stipulate what encoding the data must use. In fact, the developer can completely decide the format of the message body, as long as the final HTTP request satisfies the above format.

However, when the data is sent out, it only makes sense if the server parses it successfully. General server languages ​​such as php, python, etc., as well as their frameworks, have built-in functions to automatically parse common data formats. The server usually learns how the message body in the request is encoded according to the Content-Type field in the request header, 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 following is the official introduction to them.

application/x-www-form-urlencoded

This should be the most common way of POSTing data. The browser's native <form> form, if the  enctype attribute is not set, the data will eventually be submitted in the form of application/x-www-form-urlencoded. The request looks like this (irrelevant request headers are omitted from this article):

BASHPOST 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, the Content-Type is specified as application/x-www-form-urlencoded; secondly, the submitted data is encoded in the manner of key1=val1&key2=val2, and 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.

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

multipart/form-data

This is again a common way of POST data submission. When we upload files using a form, we must make the <form> form  enctype equal to multipart/form-data. Just look at an example request:

BASHPOST 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--

这个例子稍微复杂点。首先生成了一个 boundary 用于分割不同的字段,为了避免与正文内容重复,boundary 很长很复杂。然后 Content-Type 里指明了数据是以 multipart/form-data 来编码,本次请求的 boundary 是什么内容。消息主体里按照字段个数又分为多个结构类似的部分,每部分都是以 --boundary 开始,紧接着是内容描述信息,然后是回车,最后是字段具体内容(文本或二进制)。如果传输的是文件,还要包含文件名和文件类型信息。消息主体最后以 --boundary-- 标示结束。关于 multipart/form-data 的详细定义,请前往 rfc1867 查看。

这种方式一般用来上传文件,各大服务端语言对它也有着良好的支持。

上面提到的这两种 POST 数据的方式,都是浏览器原生支持的,而且现阶段标准中原生 <form> 表单也只支持这两种方式(通过 <form> 元素的 enctype 属性指定,默认为application/x-www-form-urlencoded。其实 enctype 还支持 text/plain,不过用得非常少)。

随着越来越多的 Web 站点,尤其是 WebApp,全部使用 Ajax 进行数据交互之后,我们完全可以定义新的数据提交方式,给开发带来更多便利。

application/json

application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。由于 JSON 规范的流行,除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify,服务端语言也都有处理 JSON 的函数,使用 JSON 不会遇上什么麻烦。

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

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

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

最终发送的请求是:

BASHPOST 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 请求是这样的:

HTMLPOST 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>

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

本文链接:https://imququ.com/post/four-ways-to-post-data-in-http.html参与评论 »

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326923707&siteId=291194637