# NestJS Request | Param, Body, Query, Headers, IP

In NestJS, 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 a NestJS Request.

### 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:**
        

```javascript
 @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:**
        

```javascript
   @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:**
    

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

If the URL is `/example?name=`hasabTech, the `getQuery` method will receive `{name=:hasabTech}`

### 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:**
    
    ```javascript
      @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:**
    
    ```javascript
     @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

```javascript
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 **NestJS** application by providing a clean and structured way to access request parameters, body, headers, IP address, and host parameters.

If you’re a visual learner, check out the following video tutorial for this blog:

[https://www.youtube.com/watch?v=0EuhRfJn0Aw&embeds\_referring\_euri=https%3A%2F%2Fdev.to%2F&source\_ve\_path=MjM4NTE](https://www.youtube.com/watch?v=0EuhRfJn0Aw&embeds_referring_euri=https%3A%2F%2Fdev.to%2F&source_ve_path=MjM4NTE)

  
***Follow us for more such content:***

[**https://www.linkedin.com/company/hasabtech**](https://www.linkedin.com/company/hasabtech)

[https://www.youtube.com/@hasabTech](https://www.youtube.com/@hasabTech)

[https://www.instagram.com/hasab.tech/](https://www.instagram.com/hasab.tech/)

[https://www.facebook.com/hasabTech](https://www.facebook.com/hasabTech)
