So, I have the following code in my server.js file that I run with node.js. I use an expression to handle HTTP requests.
app.post('/api/destinations', function (req, res) { var new_destination = req.body; console.log(req.body); console.log(req.headers); db.Destination.create(new_destination, function(err, destination){ if (err){ res.send("Error: "+err); } res.json(destination); }); });
In the terminal, I run the following:
curl -XPOST -H "Content-Type: application/json" -d '{"location": "New York","haveBeen": true,"rating": 4}' http://localhost:3000/api/destinations
After starting server.js displays the following data.
{} { host: 'localhost:3000', 'user-agent': 'curl/7.43.0', accept: '*/*', 'content-type': 'application/json', 'content-length': '53' }
So req.body is {} . I read other stack overflow reports about similar issues when the content type was incorrect due to body-parser. But this is not a problem, because the content-type is application / json.
Any ideas on how to get the actual request body?
Thanks in advance.
Charlie fish
source share