HttpRequest in the four common ContentType [Reserved] HttpRequest in the four common ContentType [Reserved]

HttpRequest in the four common ContentType [Reserved]

 

This switched: http://www.aikaiyuan.com/6324.html

 

The HTTP request method specified in HTTP / 1.1 protocol has OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT these types. Wherein POST typically used to submit the data to the server, this paper discusses ways to POST submit data.

We know, HTTP transmission protocol based on ASCII code, build the application layer specifications on top of TCP / IP protocol. Specification HTTP request is divided into three sections: a status line, a request header, the message body. It resembles the following:

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

POST predetermined data protocol must be submitted on the message body (entity-body) but does not dictate what data must encoding used. In fact, the developer can decide the format of the message body, HTTP requests sent last as long as meet the above format can be.

However, the data is sent, but also successfully resolved the server makes sense. General service side language such as php, python, etc., as well as their framework, are built to automatically resolve common data format function. Server typically be learned message body of the request in accordance with the request header (headers) in a Content-Type field is the manner in which the encoding, and then parses the subject. So it comes to data submitted POST program includes Content-Type and a message body encoding two parts. Here's the official start of their introduction.

application/x-www-form-urlencoded

This should be the most common way to POST submit the data. Native form Form browser, if you do not set the enctype attribute, then eventually it will submit the data to application / x-www-form-urlencoded manner. Request similar to the following (independent of the request header are herein omitted):

 
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 designated as application / x-www-form-urlencoded; secondly, the data is encoded in accordance with the submitted val1 manner key1 = & key2 = val2 of, key, and have carried out a URL val transcoding. Most server-side languages ​​have very good support for this approach. For example in PHP, $ _ POST [ 'title'] can be obtained the value of title, $ _ POST [ 'sub'] can be sub array.

Many times, when we submit data using Ajax, also use this approach. For example Ajax, Content-Type defaults JQuery and QWrap are "application / x-www-form-urlencoded; charset = utf-8."

multipart/form-data

This is a common way of POST data submitted. When we use the form to upload files, you must let the form of enctyped equal to this value. A direct view of exemplary request:

 
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 slightly more complex. First generates a boundary for dividing the different fields, and the body in order to avoid duplicate content, very long and complicated boundary. Then in the Content-Type indicates the data is mutipart / form-data is encoded, boundary this request is what. According to the number of fields in the message body is divided into a plurality of similar structural parts, each part are based -boundary start, followed by the content description information, then the transport, the last field is the specific content (text or binary). If the file is transmitted, but also contains the file name and file type information. The message body and end with -boundary- marked. Detailed definition of mutipart / form-data, go to rfc1867 view.

This approach is generally used to upload files, the major server-side language it also has a good support.

The two above-mentioned manner POST data, are native browser support, but at this stage the native form forms only supports these two methods. But as more and more Web sites, especially WebApp, after all use Ajax data exchange, we can define new data is presented in a way, to the developers bring more convenience.

application/json

application / json this Content-Type response header as I am sure you are not familiar with. In fact, now more and more people regard it as a request header that tells the server the message body is serialized JSON string. Due to popular JSON specification, in addition to the low of the major versions of IE browsers native support JSON.stringify, server-side languages ​​have handlers JSON using JSON will not encounter any trouble.

JSON format support structured data much more complex than the keys, it is also useful. I remember doing a hierarchical data project a few years ago, you need to submit a very deep, and I'm JSON serialization of the data submitted later. But at the time I was put as a JSON string val, remains on the key-value pairs in order to x-www-form-urlencoded submission.

Ajax functionality in Google's AngularJS, the default is to submit a JSON string. For example the following code:

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

Final request is sent:

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

This embodiment may be conveniently submitted to complex data structures, especially for RESTful interface. Major capture tool comes as Chrome Developer Tools, Firebug, Fiddler, will demonstrate JSON data in a tree structure, very friendly. But some server-side language is not yet supported in this way, for example php can not get content from the above request by $ _POST object. At this time, you need to handle yourself: when the request header Content-Type is application / json, from php: // input to obtain the original input stream in, and then json_decode to objects. Some php framework has begun to do.

AngularJS course can also be configured to submit data using x-www-form-urlencoded manner. If necessary, you can refer to this article.

text/xml

Mentioned XML-RPC (XML Remote Procedure Call) before I blog. It is using HTTP as the transport protocol, XML as the encoding of the remote calling convention. A typical XML-RPC request is such that:

 
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 >

XML-RPC protocol is simple, functional enough to achieve a variety of languages ​​are. Its use is very broad, such as the WordPress XML-RPC Api, search engines ping services. JavaScript, there are ready-made library supports data exchange in this way, can well support existing XML-RPC service. However, I personally feel that XML structure is still too bloated, general scene in JSON would be more flexible and convenient.

This switched: http://www.aikaiyuan.com/6324.html

 

The HTTP request method specified in HTTP / 1.1 protocol has OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT these types. Wherein POST typically used to submit the data to the server, this paper discusses ways to POST submit data.

We know, HTTP transmission protocol based on ASCII code, build the application layer specifications on top of TCP / IP protocol. Specification HTTP request is divided into three sections: a status line, a request header, the message body. It resembles the following:

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

POST predetermined data protocol must be submitted on the message body (entity-body) but does not dictate what data must encoding used. In fact, the developer can decide the format of the message body, HTTP requests sent last as long as meet the above format can be.

但是,数据发送出去,还要服务端解析成功才有意义。一般服务端语言如 php、python 等,以及它们的 framework,都内置了自动解析常见数据格式的功能。服务端通常是根据请求头(headers)中的 Content-Type 字段来获知请求中的消息主体是用何种方式编码,再对主体进行解析。所以说到 POST 提交数据方案,包含了 Content-Type 和消息主体编码方式两部分。下面就正式开始介绍它们。

application/x-www-form-urlencoded

这应该是最常见的 POST 提交数据的方式了。浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据。请求类似于下面这样(无关的请求头在本文中都省略掉了):

 
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

首先,Content-Type 被指定为 application/x-www-form-urlencoded;其次,提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 都进行了 URL 转码。大部分服务端语言都对这种方式有很好的支持。例如 PHP 中,$_POST[‘title’] 可以获取到 title 的值,$_POST[‘sub’] 可以得到 sub 数组。

很多时候,我们用 Ajax 提交数据时,也是使用这种方式。例如 JQuery 和 QWrap 的 Ajax,Content-Type 默认值都是「application/x-www-form-urlencoded;charset=utf-8」。

multipart/form-data

这又是一个常见的 POST 数据提交的方式。我们使用表单上传文件时,必须让 form 的 enctyped 等于这个值。直接来看一个请求示例:

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

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

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

上面提到的这两种 POST 数据的方式,都是浏览器原生支持的,而且现阶段原生 form 表单也只支持这两种方式。但是随着越来越多的 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 字符串。例如下面这段代码:

 
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
 
<?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 会更灵活方便。

Guess you like

Origin www.cnblogs.com/bwdblogs/p/11071989.html