Search code examples
jsonnestjsx-www-form-urlencoded

Nestjs accepting also application/x-www-form-urlencoded


We have a service which is calling our nestjs microservice with header Content-Type: application/x-www-form-urlencoded which seems not to be parsed as expected.

If we start also from a clean nestjs project and put this pice of code in the AppController

  @Post()
  async store(@Body() request: any) {
    console.log('request', request);
  }

If we send data to the service with curl in this way:

curl -d '{"abc": 123 }' -H 'Content-Type: application/x-www-form-urlencoded' -X POST http://localhost:3000

At the end our console.log shows as that we don't have a valid json, the whole content of the body is puted in the first parameter of the request json, which is resulting in this

request { '{"abc": 123 }': '' }

As you can see the content is not parsed right to the json, the documentation is not showing a lot of the parser, but googling this should work out of the bax

Can someone help?


Solution

  • Either post

    • URL encoded data and claim it is URL encoded data or
    • JSON encoded data and claim it is JSON encoded data

    You're posting JSON and claiming it is URL encoded, which doesn't make sense.