我想做的是使用 OctoPrint Rest API。但是,当我尝试执行需要命令的 POST 请求时,我不断遇到错误。这是文档中的一个示例。
POST /api/connection HTTP/1.1
Host: example.com
Content-Type: application/json
X-Api-Key: abcdef...
{
"command": "connect",
"port": "/dev/ttyACM0",
"baudrate": 115200,
"printerProfile": "my_printer_profile",
"save": true,
"autoconnect": true
}
我最困惑的是我在哪里包含命令或端口值。这就是我目前正在做的事情。
var self = this;
request({
url: OCTOIP + "/api/connection",
method: "POST",
"content-type": "application/json",
headers: {
"X-Api-Key": KEY
},
body: JSON.stringify({"command": "connect"})
}, function(error, response, body) {
//var info = JSON.parse(body);
if (error) {
console.log(error);
self.tell("Could Not Connect");
}
else {
console.log(body);
self.tell("Connected to printer.");
}
});
发生的情况是我不断收到此错误消息。
Expected content-type JSON
我尝试将 content-type
更改为 json
,我尝试摆脱 JSON.stringify
并只是将它为 {"command": "connect"}
。或者甚至完全删除大括号。我尝试的另一件事是使用形式而不是 body 。这些都不起作用。我在这里做错了什么?
最佳答案
已解决问题。事实证明它不起作用,因为我没有将 content-type
放入 header 中。
关于node.js - 如何通过 OctoPrint Rest API 传递命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266688/