python处理http post payload request类型请求,pythonpayload,普通的http的post


普通的http的post请求的请求content-type类型是:Content-Type:application/x-www-form-urlencoded, 如下是一个这种请求的http request头信息文本:

POST /hello-web/say HTTP/1.1Content-Length: 54Content-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:27Content-Type:application/jsonHost:localhost:8080User-Agent:Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36X-Requested-With:XMLHttpRequest

请注意其Content-Type为application/json, 他的请求数据如下格式:

{"id":12,"name":"xiaoding"}

是标准的json格式,使用chrome浏览器的开发人员工具可以看到payload请求和普通post请求的区别,如下是payload请求的截屏显示:

chrome request 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.textu'{"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. */@Controllerpublic 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项目代码可以下载。

评论关闭