我有这个curl请求有效,我希望把它变成node.js代码。
curl -H "Content-Type: application/json" -X POST -d '{"text":"ibm","date":{"from":"20170330","to":"20170530"},"restrictedDateRange":false}' https://finsights.mybluemix.net/api/query
然而,我试着按照自己的方式行事,但我确信自己做错了,因为回应机构与我从卷曲请求中得到的不符。
我的代码失败了:
server.get('/hello', function create(req, res, next) {
// //sample request
request({
url: "https://finsights.mybluemix.net/api/query",
method: "POST",
headers: {
"content-type": "application/json",
data: {
"text": "ibm",
"date": {
"from": "20170330",
"to": "20170530"
},
"restrictedDateRange": "false",
}
},
json: true, // <--Very important!!!
},
function(err, res, body) {
console.log(body);
});
// //end of sample request
console.log("success");
return next();
});
请有人教我如何改造吗?
答案 0 :(得分:2)
您的数据不应是标题。尝试在请求正文中包含您的数据,如下所示:
request({
url: "https://finsights.mybluemix.net/api/query",
method: "POST",
headers: {
"content-type": "application/json"
},
body: {
"text": "ibm",
"date": {
"from": "20170330",
"to": "20170530"
},
"restrictedDateRange": "false",
},
json: true
},
function(err, res, body) {
console.log(body);
});