I'm trying to do two different project request. I am request a using request module other project using express but json parse error in the express project
example object
var data= {
User: {
ID: 123
},
Text: 'hello world'
};
request.post({
headers: { 'content-type': 'application/x-www-form-urlencoded' },
url: "url/test",
body: JSON.stringify(data)
}, function (error, response, body) {
logger.debug("error : ", error);
logger.debug("body : ", body);
});
Listen a express project
app.post('/test', function(req, res) {
try {
res.header('Access-Control-Allow-Origin', req.headers.origin || "*");
res.header('Access-Control-Allow-Methods', 'POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
console.log(req.body);
var x = JSON.parse(req.body);
res.send(200);
} catch (error) {
res.send(200);
}
});
req.body is
{ '{"User":{"ID":123},"Text":"hello world"}': '' }
erorr is
SyntaxError: Unexpected token o in JSON at position 1
beats extra single quote { '{"User":{"ID":123},"Text":"hello world"}': '' }