I am using Node.js and the request module. I am trying to make a post request api(restfull) but it's not sending the request correctly. I can make this work in curl and in python's request module, but not the node.js request module:
var request = require('request');
token = 'sfgsfsf';
var options = {
url: 'https://_rest_full_api
headers: {
'X-Auth-Token': token
},
body: {
'status' : 'pending'
},
json: true,
method: 'put'
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info);
console.log(info);
} else {
console.log(response.statusCode);
console.log(response.body);
}
}
request(options, callback);
SyntaxError: Unexpected token o
at Object.parse (native)
at Request.callback [as _callback] (/home/one/try.js:19:25)
at Request.self.callback (/home/one/node_modules/request/request.js:122:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (/home/one/node_modules/request/request.js:888:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage.<anonymous> (/home/one/node_modules/request/request.js:839:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
With the json option set to true, request is automatically parsing the body for you to an object. You are re-parsing the body with this line:
var info = JSON.parse(body)
When you try to parse an object, you get that message:
$ node
> var t = {};
> JSON.parse(t);
SyntaxError: Unexpected token o
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With