简体   繁体   English

nodejs 请求正文为 json

[英]nodejs request body as json

I have the following curl:我有以下 curl:

curl 'http://localhost:9008/updated' \ 
  -H 'Connection: keep-alive' \
  -H 'Accept: application/json, text/plain, /' \ 
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36' \ 
  -H 'Content-Type: application/x-www-form-urlencoded' \ 
  -H 'Accept-Language: es-ES,es;q=0.9' \ 
  -data-raw '{"data":[{"id":"12345"}]}'

In NodeJS, I receive the req.body as an object:在 NodeJS 中,我收到req.body作为 object:

{ '{"data":[{"id":"12345"}]}' }

but I should receive as a JSON like in the curl:但我应该像 curl 一样收到 JSON:

{"data":[{"id":"12345"}]}

This is my handler:这是我的处理程序:

router.post('/updated', express.json(), function (req, res) {
   logger.info(req.body) // output as object, and It should be a JSON
});

Any ideas to remove the extra { or receive a JSON like in the curl?有什么想法可以删除额外的{或接收 curl 中的 JSON 吗?

Try changing Content-Type: application/x-www-form-urlencoded to Content-Type: application/json in your curl command尝试在您的 curl 命令中将Content-Type: application/x-www-form-urlencoded更改为Content-Type: application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM