码迷,mamicode.com
首页 > Web开发 > 详细

http post by requests

时间:2018-01-25 11:01:50      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:请求   3.2   ast   self   content   post请求   inf   header   code   

普通post请求

>>> import requests
>>> url = ‘http://httpbin.org/post‘
>>> payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
>>> r = requests.post(url, data=payload)
>>> print(r.text)
{
  ...
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  ...
}

若POST的数据是字典将其用双引号转成字符串

>>> payload = {‘data‘: "{‘a‘:{‘b‘:‘c‘,‘d‘:‘e‘},‘f‘:[{‘g‘:‘h‘,‘i‘:‘j‘}]}"}
>>> r = requests.post(url, data=payload)
>>> r.text
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "data": "{‘a‘:{‘b‘:‘c‘,‘d‘:‘e‘},‘f‘:[{‘g‘:‘h‘,‘i‘:‘j‘}]}"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "126",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4"
  },
  "json": null,
  "origin": "113.201.61.106",
  "url": "http://httpbin.org/post"
}

在server端(django1.5.1)以下使用以下代码可获取数据

req_data = self.request.get(‘data‘, "")
logging.info("Src json string: %s" % str(req_data))
req_obj = simplejson.loads(req_data)

参考链接:
post

http post by requests

标签:请求   3.2   ast   self   content   post请求   inf   header   code   

原文地址:http://blog.51cto.com/chaoyuezhangsan/2064898

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!