http post by requests

普通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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章