I have an node js code like this:
app.post('/', function(request, response){
var data = JSON.parse(request.body)
});
the line :
var data = JSON.parse(request.body)
does parse the request.
Using curl I do this:
curl -d '{"operation":"test"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
It always return this error:
SyntaxError: Unexpected token o
at Object.parse (native)
at /Users/admin/programs/node/test.js:28:18
at Layer.handle [as handle_request]
whats wrong here?
This is enough
var data = request.body;
req.body is already parsed. You don't need to parse it. you are applying JSON.parse on a object rather than a string. Thats the reason for the error you are getting.