简体   繁体   中英

NestJS REST controller not picking up @Body from PATCH request

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM