HTTP请求中的form data和request payload的区别

来源:互联网 发布:sql语句编写面试题 编辑:程序博客网 时间:2024/05/21 22:19

原文转载自:http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/

HTTP请求中的form data和request payload的区别

AJAX Post请求中常用的两种传参数的形式:form data 和 request payload

Form data

get请求的时候,我们的参数直接反映在url里面,形式为key1=value1&key2=value2形式,比如:

1
http://news.baidu.com/ns?word=NBA&tn=news&from=news&cl=2&rn=20&ct=1

如果是post请求,那么表单参数是在请求体中,也是以key1=value1&key2=value2的形式在请求体中。通过chrome的开发者工具可以看到如下:

123456789101112131415161718192021222324252627
RequestURL:http://127.0.0.1:8080/test/test.doRequest Method:POSTStatus Code:200 OK Request HeadersAccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en;q=0.6AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2Cache-Control:max-age=0Connection:keep-aliveContent-Length:25Content-Type:application/x-www-form-urlencodedCookie:JSESSIONID=74AC93F9F572980B6FC10474CD8EDD8DHost:127.0.0.1:8080Origin:http://127.0.0.1:8080Referer:http://127.0.0.1:8080/test/index.jspUser-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36 Form Dataname:mikanaddress:street Response HeadersContent-Length:2Date:Sun, 11 May 2014 11:05:33 GMTServer:Apache-Coyote/1.1

这里要注意post请求的Content-Type为application/x-www-form-urlencoded(默认的),参数是在请求体中,即上面请求中的Form Data。

前端:

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

在servlet中,可以通过request.getParameter(name)的形式来获取表单参数。

12345678910
/** * 获取httpRequest的参数 *  * @param request * @param name * @return */protected String getParameterValue(HttpServletRequest request, String name) {    return StringUtils.trimToEmpty(request.getParameter(name));}

Request payload

如果使用原生AJAX POST请求的话,那么请求在chrome的开发者工具的表现如下,主要是参数在

123456789101112131415161718192021222324252627282930
Remote Address:192.168.234.240:80Request URL:http://tuanbeta3.XXX.com/qimage/upload.htmRequest Method:POSTStatus Code:200 OKRequest HeadersAccept:application/json, text/javascript, */*; q=0.01Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en;q=0.6Connection:keep-aliveContent-Length:151Content-Type:application/json;charset=UTF-8Cookie:JSESSIONID=E08388788943A651924CA0A10C7ACAD0Host:tuanbeta3.XXX.comOrigin:http://tuanbeta3.XXX.comReferer:http://tuanbeta3.XXX.com/qimage/customerlist.htm?menu=19User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36X-Requested-With:XMLHttpRequestRequest Payload[{widthEncode:NNNcaXN, heightEncode:NNNN5NN, displayUrl:201409/03/66I5P266rtT86oKq6,…}]Response HeadersConnection:keep-aliveContent-Encoding:gzipContent-Type:application/json;charset=UTF-8Date:Thu, 04 Sep 2014 06:49:44 GMTServer:nginx/1.4.7Transfer-Encoding:chunkedVary:Accept-Encoding

注意请求的Content-Type为application/json;charset=UTF-8,而请求表单参数在Request Payload中。

后端获取(这里使用org.apache.commons.io.):

12345678910
/** * 从 request 获取 payload 数据 * * @param request * @return * @throws IOException */private String getRequestPayload(HttpServletRequest request) throws IOException {    return IOUtils.toString(request.getReader());}

二者区别

参考:http://stackoverflow.com/questions/10494574/what-is-the-difference-between-form-data-and-request-payload

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

 Sep 4th2014 6:57 pm  web


0 0
原创粉丝点击