Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 modeExit fullscreen mode

In this example, the:id in the route will be captured and passed to thegetParam 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 modeExit fullscreen mode

This example expects a JSON object with a keydata 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 modeExit fullscreen mode

If the URL is/example?name=Shameel, thegetQuery 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 modeExit fullscreen mode

This example extracts the value of theauthorization 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 modeExit 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')exportclassRequestController{@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 modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Tech Enthusiast, eager to learn latest trends of digital world!
  • Education
    Masters in Computer and Information Engineering
  • Work
    Software Engineer
  • Joined

More fromShameel Uddin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp