DEV Community

Shameel Uddin
Shameel Uddin

Posted on

Nest.js Request | Param, Body, Query, Headers, IP

In Nest.js, decorators are used to define metadata for various aspects of your application, including request handling. Let's break down each of the decorators for handling Nest.js Request.

Watch video explanation below

1. @Param(key?: string)

  • Description: This decorator is used to extract parameters from the request URL.
  • Usage: It can be applied to method parameters in a controller class to capture specific parameters from the URL.
  • Example:
 @Get('/param/:id')
  getParam(@Param('id') id: string) {
    return `Param ID: ${id}`;
  }
Enter fullscreen mode Exit fullscreen mode

In this example, the :id in the route will be captured and passed to the getParam method.

2. @Body(key?: string)

  • Description: Used to extract data from the request body.
  • Usage: Apply it to a method parameter to receive data from the body of a POST or PUT request.
  • Example:
   @Post()
  postBody(@Body() body: any) {
    return `Body Data: ${JSON.stringify(body)}`;
  }
Enter fullscreen mode Exit fullscreen mode

This example expects a JSON object with a key data in the request body.

3. @Query(key?: string)

  • Description: Extracts parameters from the query string in the URL.
  • Usage: Apply it to a method parameter to capture query parameters.
  • Example:
  @Get()
  getQuery(@Query() query: any) {
    return `Query Parameter:${JSON.stringify(query)}`;
  }
Enter fullscreen mode Exit fullscreen mode

If the URL is /example?name=Shameel, the getQuery method will receive {name=:shameel}.

4. @Headers(name?: string)

  • Description: Extracts values from the request headers.
  • Usage: Apply it to a method parameter to get a specific header value.
  • Example:
   @Get('/header/')
  getHeaders(@Headers() headers: any) {
    return `Header: ${JSON.stringify(headers)}`;
  }
Enter fullscreen mode Exit fullscreen mode

This example extracts the value of the authorization header.

5. @Ip()

  • Description: Retrieves the client's IP address from the request.
  • Usage: Apply it to a method parameter to get the client's IP.
  • Example:
  @Get('ip')
  getIp(@Ip() ip: string) {
    return `Client IP: ${ip}`;
  }
Enter fullscreen mode Exit fullscreen mode

This example retrieves the IP address of the client making the request.

Complete Controller Code

import {
  Body,
  Controller,
  Get,
  Ip,
  Param,
  Post,
  Query,
  Headers,
} from '@nestjs/common';

@Controller('request')
export class RequestController {
  @Get('/param/:id')
  getParam(@Param('id') id: string) {
    return `Param ID: ${id}`;
  }

  @Post()
  postBody(@Body() body: any) {
    return `Body Data: ${JSON.stringify(body)}`;
  }

  @Get()
  getQuery(@Query() query: any) {
    return `Query Parameter:${JSON.stringify(query)}`;
  }

  @Get('/header/')
  getHeaders(@Headers() headers: any) {
    return `Header: ${JSON.stringify(headers)}`;
  }

  @Get('ip')
  getIp(@Ip() ip: string) {
    return `Client IP: ${ip}`;
  }
}

Enter fullscreen mode Exit fullscreen mode

These decorators simplify the process of handling different parts of the HTTP request in your Nest.js application by providing a clean and structured way to access request parameters, body, headers, IP address, and host parameters.

Follow me for more such content:
YouTube: https://www.youtube.com/@ShameelUddin123
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

Top comments (0)