1. 程式人生 > >Form Data vs Request Payload(轉)

Form Data vs Request Payload(轉)

HTTP請求中的form data和request payload的區別
AJAX Post請求中常用的兩種傳引數的形式:form data 和 request payload
Form data
get請求的時候,我們的引數直接反映在url裡面,形式為key1=value1&key2=value2形式,比如:

如果是post請求,那麼表單引數是在請求體中,也是以key1=value1&key2=value2的形式在請求體中。通過chrome的開發者工具可以看到如下:

RequestURL:http://127.0.0.1:8080/test/test.do
Request Method:POST
Status Code:200
OK Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:zh-CN,zh;q=0.8,en;q=0.6 AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2 Cache-Control:max-age=0 Connection:keep-alive Content-Length:25 Content-Type:application/x
-www-form-urlencoded Cookie:JSESSIONID=74AC93F9F572980B6FC10474CD8EDD8D Host:127.0.0.1:8080 Origin:http://127.0.0.1:8080 Referer:http://127.0.0.1:8080/test/index.jsp User-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36 Form Data name:mikan address:street Response Headers Content-Length:2
Date:Sun, 11 May 2014 11:05:33 GMT Server:Apache-Coyote/1.1

這裡要注意post請求的Content-Type為application/x-www-form-urlencoded(預設的),引數是在請求體中,即上面請求中的Form Data。

前端:

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send("name=foo&value=bar");

Request payload

如果使用原生AJAX POST請求的話,那麼請求在chrome的開發者工具的表現如下,主要是引數在

Remote Address:192.168.234.240:80
Request URL:http://tuanbeta3.XXX.com/qimage/upload.htm
Request Method:POST
Status Code:200 OK

Request Headers
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:151
Content-Type:application/json;charset=UTF-8
Cookie:JSESSIONID=E08388788943A651924CA0A10C7ACAD0
Host:tuanbeta3.XXX.com
Origin:http://tuanbeta3.XXX.com
Referer:http://tuanbeta3.XXX.com/qimage/customerlist.htm?menu=19
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
X-Requested-With:XMLHttpRequest

Request Payload
[{widthEncode:NNNcaXN, heightEncode:NNNN5NN, displayUrl:201409/03/66I5P266rtT86oKq6,…}]

Response Headers
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json;charset=UTF-8
Date:Thu, 04 Sep 2014 06:49:44 GMT
Server:nginx/1.4.7
Transfer-Encoding:chunked
Vary:Accept-Encoding

注意請求的Content-Type為application/json;charset=UTF-8,而請求表單引數在Request Payload中。

後端獲取(這裡使用org.apache.commons.io.):

/**
 * 從 request 獲取 payload 資料
 *
 * @param request
 * @return
 * @throws IOException
 */
private String getRequestPayload(HttpServletRequest request) throws IOException {
    return IOUtils.toString(request.getReader());
}

二者區別

if a request (typically POST) has Content-type header set to application/x-www-form-urlencoded the body is expected to be in the form of a standard querystring with url-encoded key=value pairs joined by &. Form data section then shows the key-value parameters (when viewed parsed). This way was much more common in past because it is a default for HTML forms.

other cases are shown in Request payload section (and nowadays parsed for readability as well for common formats like JSON).

如果請求的Content-Type設定為application/x-www-form-urlencoded,那麼這個Post請求被認為是HTTP POST表單請求,引數出現在

其他情況如使用原生AJAX的POST請求如果不指定請求頭Request Header,預設使用的Content-Type是text/plain;charset=UTF-8,引數出現在Request payload塊。