To get all query parameter values from a GET
request, you can use the @Query()
decorator function from the @nestjs/common
module inside the parameter brackets of the controller's respective method in Nestjs.
TL;DR
import { Controller, Get, Query } from "@nestjs/common";
// basic controller for `/hello` GET endpoint
@Controller("/hello")
export class AppController {
@Get()
// 👇🏽 Using the `@Query()` decorator function to
// get all the query parameters in the request
getHello(@Query() query: { name: string }): string {
return `Hello ${query.name}`;
}
}
/*
CONCLUSION: 🎬
If we now send a `GET` request to `/hello?name=John`
we will get a response of -> `Hello John`
*/
For example, let's say we have a basic GET
controller that has an endpoint called /hello
and on requesting to it gives us a response called Hello World
like this,
import { Controller, Get } from "@nestjs/common";
// basic controller for `/hello` GET endpoint
@Controller("/hello")
export class AppController {
@Get()
getHello(): string {
return `Hello World`;
}
}
NOTE: To know more about creating a GET request in Nestjs, See the blog on How to make a simple GET request or an API endpoint in Nestjs?
Now let's say we need to have a query parameter called name
and on request to the /hello?name=John
GET
request endpoint, it should return a response of Hello John
.
So to achieve this first, we have to import the @Query()
decorator function from the @nestjs/common
module.
It can be done like this,
import { Controller, Get, Query } from "@nestjs/common";
// basic controller for `/hello` GET endpoint
@Controller("/hello")
export class AppController {
@Get()
getHello(): string {
return `Hello World`;
}
}
Now we need to use the @Query()
decorator function inside the respective function's parameters brackets.
After the decorator function, we can write a variable name let's call it query
where it will contain the query parameters values from the request as an object
type. After the query parameter
object variable, we can write the type for each query parameter in the request.
It can be done like this,
import { Controller, Get, Query } from "@nestjs/common";
// basic controller for `/hello` GET endpoint
@Controller("/hello")
export class AppController {
@Get()
// 👇🏽 Using the `@Query()` decorator function to
// get all the query parameters in the request
getHello(@Query() query: { name: string }): string {
return `Hello World`;
}
}
Now after defining the query
query parameters object variable, let's now use the name
query parameter in the return statement of the method so that we will get a response of Hello <name>
when we request to /hello
GET
request endpoint with the name
query parameter.
It can be done like this,
import { Controller, Get, Query } from "@nestjs/common";
// basic controller for `/hello` GET endpoint
@Controller("/hello")
export class AppController {
@Get()
// 👇🏽 Using the `@Query()` decorator function to
// get all the query parameters in the request
getHello(@Query() query: { name: string }): string {
return `Hello ${query.name}`;
}
}
/*
CONCLUSION: 🎬
If we now send a `GET` request to `/hello?name=John`
we will get a response of -> `Hello John`
*/
Now if we send a GET
request to /hello?name=John
we will get a response that looks like this,
Hello John
We have successfully got all the query parameters from a GET
request in Nestjs. Yay 🥳!
See the above code live in codesandbox.
That's all 😃!