visual-studio-coderfc2616vscode-restclient

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


I have this test request using the VS Code REST Client extension:

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

content-type: application/json

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

On the receiving end, the NestJS app listening on this endpoint isn't picking up the body of the PATCH request, so there's no update within the NestJS service responsible for update requests.

Here's the controller method/endpoint in NestJS:

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

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

And the result of the debugging console log above:

body:  {}

Thanks!


Solution

  • The extra line between the URI request and the request headers was the problem. One must put the request headers directly after the URI as such:

    PATCH http://localhost:3000/auth/3
    content-type: application/json
    
    { "email": "new.email3@gmail.com" }
    

    Reading the section on request headers on the VS Code REST Client Extension page, the author clearly states: The lines immediately after the request line to first empty line are parsed as Request Headers

    Then the body comes through as expected:

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

    Hope this saves someone else the time it took me to figure it out.