Node.js express.json中间件不按预期parsing请求

我有一个简单的cURL(我认为这是正确的),发布一个小的JSON对象到我的快递服务器:

curl -d "{'test': 'this'}" localhost:3000/rest/user/authenticate 

我有明确的设置为:

 // set up body parsing in express to be able to get parse JSON posts server.use(express.json()); server.use(express.urlencoded()); 

并有接受路由的处理程序:

 JSON = require('JSON') module.exports = { authenticateUser: function create(req, res){ var postedObject = req.body console.log(postedObject) res.send('Handle Post: authenticateUser'); } } 

该处理程序正在调用,但它意外地loggingJSON正文:

 { '{\'test\': \'this\'}': '' } 

所以我的整个对象看起来是JSON名称:值对对象的名称端。 无论我发布什么,似乎都是追加价值的一面。 除非我做这样的事情:

 curl -d "a=a" localhost:3000/rest/user/authenticate 

日志logging:

 {'a':'a'} 

所以我没有设置正确的标题? configurationexpression错误? 我打算通过快速代码进行挖掘,但是想知道在find解决scheme之前是否有人知道。 无论哪种方式在网上有一个可search/索引的答案将是很好的。

更新1

好吧,我需要将标题添加到cURL

 curl -H "Content-Type: application/json" -d "{'test': 'this'}" localhost:3000/rest/user/authenticate 

这给出了错误:

 Parsing: {'test': 'this'} SyntaxError: Unexpected token ' at Object.parse (native) at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19 at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7) at IncomingMessage.g (events.js:180:16) at IncomingMessage.EventEmitter.emit (events.js:92:17) at _stream_readable.js:920:16 at process._tickCallback (node.js:415:13) 

或者curl -H“Content-Type:application / json”-d“{test:'this'}”localhost:3000 / rest / user / authenticate

这给出了错误:

 Parsing: {test: 'this'} SyntaxError: Unexpected token t at Object.parse (native) at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19 at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7) at IncomingMessage.g (events.js:180:16) at IncomingMessage.EventEmitter.emit (events.js:92:17) at _stream_readable.js:920:16 at process._tickCallback (node.js:415:13) 

更新2

在文件connect / lib / middleware / json.js中

这条线似乎是造成问题的一条线

 req.body = JSON.parse(buf, options.reviver); 

更新3

我真的认为这是我的cURL

 buf= JSON.stringify({test: 'This'}); console.log(buf) req.body = JSON.parse(buf, options.reviver); 

先logging日志{“test”:“this”}

然后在我的处理程序中:

 ---------------------- { test: 'this' } ---------------------- 

1)JSON中间件仅在请求具有Content-Type: application/json头时才起作用。
2)有效的JSON应该包含" ,不'
所以应该是'{"test": "this"}'而不是"{'test': 'this'}"

试试这个命令:

 curl -d '{"test": "this"}' -H "Content-Type: application/json" localhost:3000/rest/user/authenticate 

答案有两个部分

  1. 添加内容标题
  2. 在窗口上通过正确传递JSON的痛苦,我的Windows机器上的curl不处理交换单引号和双引号,因此正确的curl是

    curl -H“Content-Type:application / json”-d“{”“”test“”“:”“”this“”“}”localhost:3000 / rest / user / authenticate

是的,在数据​​参数中,您需要使用三个双引号向服务器发送一个双引号。

接受zub的答案,因为他是正确的。 我在这里尝试了一堆东西来绕过窗户。

对于那些像我这样的邮递员,只是尝试发送请求作为POST >> RAW并创build您的Json做这个职位,在我的testing中,我已经创build了一个简单的JSON这样的工作:

{“name”:“test”}

使用表单数据或x-www-form-urlencoded它不起作用。

在Windows cmd下:

 curl -H "Content-Type: application/json" -d "{"""test""": """this"""}" localhost:3000/rest/user/authenticate 

或者使用cygwin:

 curl -H "Content-Type: application/json" -d '{"test": "this"}' localhost:3000/rest/user/authenticate 

我的2美分