我在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();
});
});
当我做我的cURL时:curl -X POST -H '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment
我收到此错误:
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)
我做错了什么?! :)
谢谢!
答案 0 :(得分:1)
在curl命令中设置--header "Content-Type:application/json"
并使用-d
标志发送数据
因此:
curl -X POST -H "Content-Type:application/json" -d '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment