Nest.js Request | Param, Body, Query, Headers, IP
Published: 2023-12-30 19:06:22

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

{% youtube 0EuhRfJn0Aw %}

### 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:**
  ```typescript
 @Get('/param/:id')
  getParam(@Param('id') id: string) {
    return `Param ID: ${id}`;
  }
  ```
  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:**
  ```typescript
   @Post()
  postBody(@Body() body: any) {
    return `Body Data: ${JSON.stringify(body)}`;
  }
  ```
  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:**
  ```typescript
  @Get()
  getQuery(@Query() query: any) {
    return `Query Parameter:${JSON.stringify(query)}`;
  }
  ```
  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:**
  ```typescript
   @Get('/header/')
  getHeaders(@Headers() headers: any) {
    return `Header: ${JSON.stringify(headers)}`;
  }
  ```
  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:**
  ```typescript
  @Get('ip')
  getIp(@Ip() ip: string) {
    return `Client IP: ${ip}`;
  }
  ```
  This example retrieves the IP address of the client making the request.

## Complete Controller Code

```js
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}`;
  }
}

```


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