【发布时间】:2017-02-19 22:18:29
【问题描述】:
我无法通过 nodeJS 将有效的 curl 转换为等效的 http 请求。我正在使用 Request 模块,但在发出请求时我似乎做错了什么。当我运行它时,它给了我
body: Cannot POST /path
不确定如何调试,有什么想法吗?
var data = JSON.stringify({
'sender': {
'name': 'name',
'handle': 'handle'
},
'subject': 'Title here',
'body': 'something something',
'metadata': {}
});
var options = {
host: 'website.com',
path: '/path',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <token>',
'Accept': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
下面是我尝试为上述 nodejs 制作的等效 curl(有效)。
curl --include \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer <token>" \
--header "Accept: application/json" \
--data-binary "{
\"sender\": {
\"name\": \"name\",
\"handle\": \"handle\"
},
\"subject\": \"Title here\",
\"body\": \"something something\",
\"metadata\": {}
}" \
'website.com/path"
【问题讨论】: