NestJS REST 控制器未从 PATCH 请求中获取 @Body(VSCode REST 客户端)

NestJS REST controller not picking up @Body from PATCH request (VSCode REST Client)

我有这个使用 VS Code REST 客户端扩展的测试请求:

PATCH http://localhost:3000/auth/3

content-type: application/json

{ "email": "new.email3@gmail.com" }

在接收端,监听此端点的 NestJS 应用程序 不会 获取 PATCH 请求的主体,因此 NestJS 服务中没有负责更新的更新请求。

这是 NestJS 中的控制器 method/endpoint:

  @Patch('/:id')
  updateUser(@Param('id') id: string, @Body() body: UpdateUserDto) {
    
    console.log('body: ', body);

    return this.usersService.update(parseInt(id), body);
  }

以及上面调试控制台日志的结果:

body:  {}

谢谢!

URI 请求和 headers 请求之间的额外行是问题所在。必须将请求 headers 直接放在 URI 之后:

PATCH http://localhost:3000/auth/3
content-type: application/json

{ "email": "new.email3@gmail.com" }

阅读 VS Code REST 客户端扩展页面上 request headers 的部分,作者明确指出: The lines immediately after the request line to first empty line are parsed as Request Headers

然后 body 按预期通过:

body:  { email: 'new.email3@gmail.com' }

希望这可以为其他人节省我解决问题的时间。