Node.js express.json中间件未按预期解析请求



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

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');
}
}

正在调用处理程序,但它意外地记录了JSON正文:

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

因此,我的整个对象看起来是JSON名称:值对对象的名称端。无论我发布什么,它似乎都是附加了价值的一面。除非我这样做:

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

哪个日志:

{'a':'a'}

那么我没有设置正确的标题吗?express配置错误?我计划深入研究express代码,但不知道在我找到解决方案之前是否有人知道。无论哪种方式,在网上有一个可搜索/索引的答案都会很好。

更新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:blahnode_modulesexpressnode_modulesconnectlibmiddlewarejson.js:86:19
at IncomingMessage.onEnd (C:blahnode_modulesexpressnode_modulesconnectnode_modulesraw-bodyindex.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"内容类型:application/json"-d"{test:'this'}"localhost:3000/rest/user/authenticate

给出错误:

Parsing: {test: 'this'}
SyntaxError: Unexpected token t
at Object.parse (native)
at C:blahnode_modulesexpressnode_modulesconnectlibmiddlewarejson.js:86:19
at IncomingMessage.onEnd (C:blahnode_modulesexpressnode_modulesconnectnode_modulesraw-bodyindex.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);

首先工作日志记录{"测试":"这个"}

然后在我的处理程序中:

----------------------
{ 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. 在windows上,正确传递json是很痛苦的,我的windows机器上的cURL不处理单引号和双引号的交换,因此正确的cURL是

    curl-H"内容类型:application/json"-d"{""test":"this"}"localhost:3000/rest/user/authenticate

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

接受祖布的回答,因为他是正确的。我试了很多办法绕过窗户。

对于那些像我这样使用poster的人,只需尝试将请求发送为POST>>RAW并创建Json来完成帖子,在我的测试中,我创建了一个简单的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美分

最新更新