Minniek
Minniek

Reputation: 33

nodejs request body as json

I have the following 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:

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

but I should receive as a JSON like in the curl:

{"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?

Upvotes: 0

Views: 187

Answers (1)

Gavin Haynes
Gavin Haynes

Reputation: 1992

Try changing Content-Type: application/x-www-form-urlencoded to Content-Type: application/json in your curl command

Upvotes: 2

Related Questions