python - OctoPrint API,发出 POST 请求时出现错误 400
问题描述
我正在使用Octoprint 的 API。我正在努力尝试向 3d 打印机发出命令。
例如,我想发出一个命令,使 3d 打印机在 X 轴上点动。
import requests
headers = {"Authorization": "Bearer <token>"}
def Beep():
api_link = "http://octopi.local/api/printer/command"
params = {"command":"jog","x":10}
return requests.post(api_link, headers=headers, params=params)
此代码的输出是 <Response 400>(错误请求)。我错过了什么?
解决方案
从 API 文档:
如果没有另外说明,OctoPrint 的 API 期望请求正文和发出响应正文为
Content-Type: application/json
.
您的请求未发送 JSON。改用这个:
requests.post(api_link, headers=headers, json=params)
此外,看起来该jog
命令应该使用 url path /api/printer/printhead
。
推荐阅读
- react-native - 后台返回后调用函数
- python-3.x - 'list' 对象没有属性 'encode
- javascript - 在 TouchableHighlight 标记内包装图像标记无法正常工作
- javascript - 我正在尝试包含 simplepeer.js,但未找到显示 404 错误。相反,我给出了正确的道路
- javascript - 从 Firestore DocumentReferences 列表中获取数据到 State React
- c# - 当我从我的数据库中导入模型时,我希望我的数据库中的实例不在数据库模型中生成
- python - Django 将用户详细信息添加到 UserPost 列表视图
- json - 为令牌交换代码时获取 unsupported_grant_type
- grafana - Grafana + InfluxDB Flux - 用于显示多选变量输入的查询
- android - 当编译器没有给出任何错误时,为什么 RecyclerView 不工作?