簡體   English   中英

nodejs 請求正文為 json

[英]nodejs request body as json

我有以下 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"}]}'

在 NodeJS 中,我收到req.body作為 object:

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

但我應該像 curl 一樣收到 JSON:

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

這是我的處理程序:

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

有什么想法可以刪除額外的{或接收 curl 中的 JSON 嗎?

嘗試在您的 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