【发布时间】:2018-03-20 06:58:42
【问题描述】:
我想在 python 中获取 POST Web 服务数据。为此,我尝试了以下方法:
import requests
import json
headers = {'content-type': 'application/json','charset': 'utf-8'}
url = 'https://a.b.c.com/count/WebService.asmx/ReadJSON'
data = {'tick': '123456A123456B'}
response = requests.post(url, data=json.dumps(data), headers=headers)
print response.status_code
print response.text
以上代码输出:
200
{"a":""}
但实际上,key "a" 有一些我没有得到的价值。我不明白它给我的状态代码是 200 即好的,那么为什么我无法获得正确的 JSON 响应。我是否遗漏了代码中的某些内容?
【问题讨论】:
-
使用
response.json()而不是response.text,你不需要将你的字典转储为字符串,你可以按原样传递它 -
如果我通过了,response = requests.post(url, data=data, headers=headers) 我得到 {u'StackTrace': u'', u'Message': u'There处理请求时出错。', u'ExceptionType': u''} 错误。
标签: python post python-requests