Ismailp
Ismailp

Reputation: 2383

Getting an error on my app.post() in Express.js and Node.js

I have the following code in Node.js/Express.js

// Post a comment
app.post('/:id/comment', function(req, res){
    var snipp_id = req.params.id;
    var comment = req.body.comment;
    var line_num = req.body.line;

    models.Snipp.findById(snipp_id, function(err, snipp){
        snipp.comments.push({body: comment,line: line_num});
        res.send('OK');
        snipp.save();
    });
});

and when I do my cURL: curl -X POST -H '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment

I get this error:

SyntaxError: Unexpected token t
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/express/node_modules/connect/lib/middleware/json.js:76:27)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

What am I doing wrong?! :)

Thanks!

Upvotes: 1

Views: 849

Answers (1)

Ygg
Ygg

Reputation: 3870

set --header "Content-Type:application/json" in your curl command and use the -d flag to send data

thus:

curl -X POST -H "Content-Type:application/json" -d '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment

Upvotes: 1

Related Questions