Node.js a如何通过OctoPrint Rest API传递命令?

Node.js a如何通过OctoPrint Rest API传递命令?,node.js,rest,httprequest,octoprint,Node.js,Rest,Httprequest,Octoprint,我想做的是使用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, "pri

我想做的是使用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.");
        }
    });
发生的事情是我一直收到这个错误消息

预期的内容类型为JSON


我尝试将
内容类型
更改为仅
json
,我尝试摆脱
json.stringify
并将其作为
{“命令”:“connect”}
。甚至连卷曲的括号都扔掉了。我尝试的另一件事是用形式和代替身体。这些都不起作用。我做错了什么?

修复了这个问题。结果证明它不起作用,因为我没有在标题中添加
内容类型