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:
@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:
@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:
@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:
@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:
@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
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:
Follow us for more such content:
https://www.linkedin.com/company/hasabtech
https://www.youtube.com/@hasabTech
Subscribe to my newsletter
Read articles from hasabTech directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

hasabTech
hasabTech
HasabTech is a developer-focused educational platform committed to simplifying tech learning. We share practical coding tutorials, programming guides, and tech insights to help beginners and aspiring developers build real world skills. Our mission is to make technical education accessible, clear, and community driven. We are currently building our in house products, experimenting with real world tech solutions, and sharing what we learn along the way.