python - Python Post 请求到 API
问题描述
这可能是一个简单的问题,但我找不到为什么我无法将发布请求调用到 api url 的问题。我对类似的问题进行了交叉检查,但我的仍然有问题。
这是脚本
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 [200]>
如果我像下面那样运行 curl ok..它会从 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 ok...但是当我使用 requests/json python 时遇到问题...我想我可能会错过一些我无法检测到的东西。请帮助并指出正确的方法。谢谢你。
解决方案
我有类似的错误,转储我的数据解决了这个问题。尝试将您的身体作为垃圾场传递:
import requests
import json
#API details
url = "http://192.168.1.100:9792/api/scan"
body = json.dumps({"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.json())
推荐阅读
- algorithm - 六边形图查找是否有一些网格被包围算法
- c# - 在使用具有潜在作用域依赖关系的 Simpleinjector 时处理 Websharper Remoting Singletons 的最佳方法是什么?
- python - 递归循环没有预期的行为
- graphviz - graphviz dot 输出的边缘聚集在一起
- python-3.x - Python中正则表达式的含义
- python - 在 Python 3 中更改 Windows 10 背景
- java - 将 Spring MVC Web 应用程序转换为独立的桌面软件
- java - 驱动程序无法解决错误,而 WebDriver 是通过 Selenium 和 Java 从不同功能的 public static void main() 外部定义的
- python - allure-pytest:AttributeError:“模块”对象没有属性“附件类型”
- android-studio - 使用带有游标的“between”查询sqlite数据库