I am trying to call post data method of another application(which is running in localhost:3000) using node.js. But its returning error..
The following is the code:
var options = {
host: 'localhost',
path: '/v1/key/11111111',
//since we are listening on a custom port, we need to specify it by hand
port: '3000',
//This is what changes the request to a POST request
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(options, callback);
//This is the data we are posting, it needs to be a string or a buffer
req.write("hello world!");
req.end();
But i am receiving error:
{"meta":{"version":1,"status_code":400},"results":{"error":{"type":"Error","message":"invalid json"}}}
When i am trying using CURL command i am getting the desired result:
curl -X POST localhost:3000/v1/key/12345678 --header "Content-Type:application/json"
My question is how i have to construct POST request for the above requirement.