I am sending a POST request with cURL :
curl http://tarvos.local:8080/partial_Users/2 -d '{currentPage : 1, firstID : 53d62fc6642aecf45c8b456f }'
In my NodeJS application, the request goes through the bodyParser.json() middleware. I have another middleware that processes the request :
app.post('/partial_Users/:page', function(req, res, next) {
var destinationPage = req.params.page;
var currentPage = req.body.currentPage;
var firstID = req.body.firstID;
console.log(req.body)
console.log(''+ currentPage + firstID)
[...]
}
In this situation, I would expect req.body.currentPage to return 1. In fact, it returns 'undefined'.
I have noticed that when I do a console.log(req.body), I get the following string :
{ '{currentPage : 1, firstID : 53d62fc6642aecf45c8b456f }': '' }
It becomes apparent now that req.body.currentPage would return undefined. For some reason, bodyParser is not parsing JSON properly. The same behaviour is seen for thereq.body.firstID.
Either I am not using BodyParser properly, or my JSON request in not structured properly maybe?
You need to add -H "Content-Type: application/json" to your curl command line, otherwise application/x-www-form-urlencoded is assumed.