I am trying to create one Node js server with http package. I want to receive only the POST request that I have already completed. The problem I am facing is that I cannot parse JSON correctly (I expect one JSON to be attached).
I tried using JSON.parse, but this does not parse all json content. It leaves some values as [Object], which is incorrect. I saw several packages that are JSONStream, but I'm not sure how to implement in this case.
server.on('request', function(req, res){
if(req.method == 'POST')
{
var jsonString;
req.on('data', function (data) {
jsonString = JSON.parse(data);
});
req.on('end', function () {
serverNext(req, res, jsonString);
});
}
else
{
res.writeHead(405, {'Content-type':'application/json'});
res.write(JSON.stringify({error: "Method not allowed"}, 0, 4));
}
res.end();
});
Request example:
Here d = the contents of the JSON file. (I did this in Python to make this sample request)
r = requests.post('http://localhost:9001', headers = {'content-type': 'application/json'}, data = json.dumps(d))
Note. I can parse JSON correctly, but there are times when it shows something like this:
{ 'Heading':
{ 'Content':
{ sometext: 'value',
List: [Object],
test: [Array] } } }
Update:
serverNext() , :
var testReq = Object.keys(jsonData)[0];
var testId = Object.keys(jsonData[testRequest])[0];
var test = jsonData[testRequest][testId]
, , - [Objects] .