0
0
NestjsHow-ToBeginner ยท 3 min read

How to Use @Body Decorator in NestJS Controllers

In NestJS, use the @Body decorator inside a controller method to access the data sent in the HTTP request body. It extracts the JSON or form data so you can use it as a JavaScript object in your method.
๐Ÿ“

Syntax

The @Body() decorator is used in a controller method parameter to get the entire request body or a specific property from it.

  • @Body() without arguments extracts the whole body as an object.
  • @Body('propertyName') extracts only the value of the specified property.
typescript
import { Controller, Post, Body } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Post()
  createItem(@Body() body: any) {
    // body contains the full request body
  }

  @Post('partial')
  createPartial(@Body('name') name: string) {
    // name contains only the 'name' property from the body
  }
}
๐Ÿ’ป

Example

This example shows a simple NestJS controller that uses @Body() to receive JSON data from a POST request and returns it back in the response.

typescript
import { Controller, Post, Body } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() userData: { name: string; age: number }) {
    return {
      message: 'User created successfully',
      user: userData,
    };
  }
}
Output
{ "message": "User created successfully", "user": { "name": "Alice", "age": 30 } }
โš ๏ธ

Common Pitfalls

  • Forgetting to use @Body() and trying to access the body directly causes undefined values.
  • Not setting the correct Content-Type header (like application/json) in the request can lead to empty or missing body data.
  • Using @Body() in GET requests is not standard and may not work as expected.
typescript
import { Controller, Post } from '@nestjs/common';

@Controller('wrong')
export class WrongController {
  @Post()
  createWrong(body: any) {
    // This will be undefined because @Body() decorator is missing
    return body;
  }
}

// Correct way:
import { Controller, Post, Body } from '@nestjs/common';

@Controller('right')
export class RightController {
  @Post()
  createRight(@Body() body: any) {
    return body;
  }
}
๐Ÿ“Š

Quick Reference

Use this quick guide to remember how to use @Body in NestJS:

UsageDescription
@Body()Gets the entire request body as an object
@Body('propertyName')Gets a specific property from the request body
Works only with POST, PUT, PATCH requestsRequest must have a body and proper Content-Type
Use DTO classes for validationCombine with class-validator for safer data handling
โœ…

Key Takeaways

Use @Body() in controller methods to access HTTP request body data in NestJS.
You can extract the whole body or a specific property by passing a string to @Body().
Always ensure the client sends the correct Content-Type header like application/json.
Do not forget to add the @Body() decorator; otherwise, the body will be undefined.
Use DTOs with validation pipes for safer and cleaner data handling.