1

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!

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.