Reputation: 519
I have this curl request that worked, and I hope to turn it into node.js code.
curl -H "Content-Type: application/json" -X POST -d '{"text":"ibm","date":{"from":"20170330","to":"20170530"},"restrictedDateRange":false}' https://finsights.mybluemix.net/api/query
However, I tried in my way but i am quite sure i did something wrong as the response body doesnt match what i got from the curl request.
My code that failed:
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();
});
Please can anyone teach me how to transform it please?
Upvotes: 2
Views: 2707
Reputation: 1984
Your data shouldn't be a header. Try including your data in the request body, like this:
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);
});
Upvotes: 3