我想要做的是使用OctoPrint Rest。但是,当我试图执行需要命令的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.");
}
});结果就是我一直收到这个错误消息。
预期内容类型JSON
我尝试过将content-type更改为json,我尝试摆脱JSON.stringify并将其作为{"command": "connect"}。甚至连花括号都掉了。我试过的另一件事是用形式代替身体。所有这些都没有奏效。我在这里做错什么了?
发布于 2018-03-14 12:26:48
解决了这个问题。结果发现它不起作用,因为我没有把content-type放在头上。
https://stackoverflow.com/questions/49266688
复制相似问题