Nestjs 也接受 application/x-www-form-urlencoded

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

我们有一项服务正在使用 header Content-Type: application/x-www-form-urlencoded 调用我们的 nestjs 微服务,但似乎未按预期进行解析。

如果我们也从一个干净的 nestjs 项目开始并将这段代码放入 AppController

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

如果我们这样用curl向服务发送数据:

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

最后我们的 console.log 显示我们没有有效的 json,body 的全部内容放在请求的第一个参数中 json,结果是

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

如您所见,内容未正确解析 json,文档未显示很多解析器,但谷歌搜索应该可以解决 bax

有人可以帮忙吗?

或者post

  • URL编码数据并声称它是URL编码数据
  • JSON 编码数据并声称它是 JSON 编码数据

您 posting JSON 并声称它是 URL 编码的,这没有意义。