@Body()
Extracts the request body or a specific body property into a handler parameter.
Syntax
@Body(propertyOrPipe?, ...pipes) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
property | string | No | Optional key to extract a single property from the body. |
pipes | PipeTransform[] | No | Optional pipes applied to the extracted value. |
Returns
ParameterDecorator — A decorator that binds the body to the parameter.
Examples
import { Controller, Post, Body } from '@nestjs/common';
class CreateUserDto {
email: string;
password: string;
}
@Controller('users')
export class UsersController {
@Post()
create(@Body() dto: CreateUserDto) {
return dto.email;
}
@Post('login')
login(@Body('email') email: string) {
return email;
}
}
Notes
The body is parsed by the platform body-parser. With a DTO class and the
global ValidationPipe, incoming payloads are validated and transformed
before reaching the handler.