我想在 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":""}
但实际上,键“a”有一些我没有得到的值。我不明白它给我的状态代码为 200 即 OK 那么为什么我无法获得正确的 JSON 响应。我是否遗漏了代码中的某些内容?
最佳答案
您应该使用json=data
在请求中传递json,但不要手动修改 header ,如果您这样做,您应该使用response.json()
来获取json结果当然是。
import requests
headers = {'charset': 'utf-8'}
url = 'https://a.b.c.com/count/WebService.asmx/ReadJSON'
data = {'tick': '123456A123456B'}
response = requests.post(url, json=data, headers=headers)
print response.status_code
print response.json()
关于python - 如何在 python 中发送 POST 请求并获得正确的 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640140/