这可能是一个简单的问题,但我找不到原因为什么我无法调用API网址的POST请求。我已经与类似问题进行了交叉检查,但我的问题仍然存在。
这是脚本:
这是脚本:
import requests
import json
#API details
url = "http://192.168.1.100:9792/api/scan"
body = {"service":"scan", "user_id":"1", "action":"read_all", "code":"0"}
headers = {'Content-Type': 'application/json'}
#Making http post request
response = requests.post(url, headers=headers, data=body, verify=False)
print(response)
#Decode response.json() method to a python dictionary for data process utilization
dictData = response.json()
print(dictData)
with open('scan.json', 'w') as fp:
json.dump(dictData, fp, indent=4, sort_keys=True)
出现错误
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
打印(response)得到了返回值
<Response [200]>
如果我按照以下方式运行curl,它会正常工作。它将返回API POST请求的数据。
curl --header "Content-Type: application/json" --request POST --data '{"service":"scan","user_id":"1","action":"read_all","code":"0"}' http://192.168.1.100:9792/api/scan
使用curl没问题,但当我使用requests/json python时出现了问题……我想我可能漏掉了某些东西,我无法检测到。请帮忙指点正确的方向。谢谢。
response.text
包含什么内容? - Abdul Niyas P M