I been following this tutorial on creating a simple API with Node.js and MongoDB, however I've run into some issues with the formatting of the JSON post that I'm making.
The post is something along the lines of:
curl POST -d '{"test":"test"}' http://127.0.0.1:3000/add
However once in MongoDB it looks like the following:
{\"test\":\"test\"}":"","_id":"4ff725d6349fdf9c0d000004"}
My POST function is as follows:
app.post('/add', function(req, res){
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('docs', function(err, coll){
coll.insert( req.body, {safe:true}, function(err){
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(req.body));
});
});
});
});
Any ideas on how I can ensure the POST is processed correctly would be helpful!
Thanks in advance!
Looks like it may be parsing your POST data as form encoded. Try this CURL command:
curl -H "Content-Type: application/json" -X POST -d '{"test":"test"}' http://127.0.0.1:3000/add