首页 > 解决方案 > 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>(错误请求)。我错过了什么?

标签: pythonapipython-requestsoctoprint

解决方案


从 API 文档:

如果没有另外说明,OctoPrint 的 API 期望请求正文和发出响应正文为Content-Type: application/json.

您的请求未发送 JSON。改用这个:

requests.post(api_link, headers=headers, json=params)

此外,看起来该jog命令应该使用 url path /api/printer/printhead


推荐阅读