普通的http的post請求的請求content-type類型是:Content-Type:application/x-www-form-urlencoded
,
如下是一個這種請求的http request頭信息文本:
POST /hello-web/say HTTP/1.1 Content-Length: 54 Content-Type: application/x-www-form-urlencoded
請注意上面Content-Type部分, 其form數據的內容爲 data=%7B%22id%22%3A12%2C%22name%22%3A%22xiaoding%22%7D
是經過urlencode的鍵值對形式。
而另外一種形式request payload的http請求頭的形式爲:
Content-Length:27 Content-Type:application/json Host:localhost:8080 User-Agent:Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36 X-Requested-With:XMLHttpRequest
請注意其Content-Type爲application/json, 他的請求數據如下格式:
{"id":12,"name":"xiaoding"}
是標準的json格式,使用chrome瀏覽器的開發人員工具可以看到payload請求和普通post請求的區別,如下是payload請求的截屏顯示:
使用jquery的ajax方法,指定http header可以方便的發起payload的請求。
例如:
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!doctype html>
<html>
<head>
<title>hello spring</title>
</head>
<body>
<h2>Hello Spring.</h2>
<form method="post" action="./say">
<textarea name="data" cols="150" rows="2">{"id":12,"name":"xiaoding"}</textarea>
<p>
<button type="button">提交</button>
</p>
</form>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function(){
$('button[type="button"]').click(function(o){
var data = $('textarea[name="data"]:first').val();
$.ajax({
'type': 'POST',
'url': './say',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
'data': data,
'dataType': 'json',
'success': function(d){
alert(d.success);
}
});
});
});
</script>
</body>
</html>
請注意ajax的data是ajax內容,而非鍵值對, ajax函數還設置了自定義頭。
在python中可以通過requests包方便的發起payload請求:
>>> import requests, json
>>> response = requests.post('http://localhost:8080/hello-web/say', data=json.dumps({'id':1,'name':'names'}),headers={'content-type':'application/json'})
>>> response.text
u'{"hello.name":"names","success":"true"}'
requests.post指定的data數據爲json.dumps方法的結果數據,並設置了headers參數指定Content-Type爲application/json。
最後爲了調試方便,附贈一段spring mvc寫的接受request payload參數的處理controller程序:
package cn.outofmemory.controller;
import cn.outofmemory.model.HelloInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* Created by yukaizhao on 2015/8/24.
*/
@Controller
public class Hello {
@RequestMapping(value="/", method= RequestMethod.GET)
public ModelAndView showIndex() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
@RequestMapping(value="/say", method=RequestMethod.POST, consumes = "application/json")
public @ResponseBody Map<String, String> say(@RequestBody HelloInfo helloInfo) {
Map<String, String> response = new HashMap<String, String>();
response.put("success", Boolean.TRUE.toString());
response.put("hello.name", helloInfo.getName());
return response;
}
}
請看say方法, 完整的java項目代碼可以下載。
<轉自:http://outofmemory.cn/code-snippet/37910/python-requests-request-http-post-payload-method>