I am using node.js and trying to parse the JSON body of a request.
I am getting the following error:
undefined:0
^
SyntaxError: Unexpected end of input
at Object.parse (native)
at IncomingMessage.<anonymous> (C:\node\xxxx.js:36:14)
at IncomingMessage.emit (events.js:64:17)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:130:23)
at Socket.ondata (http.js:1506:22)
at TCP.onread (net.js:374:27)
I am doing:
request.on('data', function(chunk)
{
data+=chunk;
});
// and in the end I am doing
obj = JSON.parse(data); // it's complaining at this point.
input is:
{
"result": "success",
"source": "chat"
}
You’re trying to parse the data before it is completely recieved…put your JSON.parse inside the .end method of request