How to get all query parameters from a GET request in Nestjs?
Tags: resource
Published: 2022-01-30 00:00:00

---
  title: How to get all query parameters from a GET request in Nestjs?
  published: true
  tags: Resource
  date: Sun Jan 30 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/get-all-query-parameters-in-get-request-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-8ad98d9ecbe085394740294f2e377be1.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/get-all-query-parameters-in-get-request-nestjs)
  
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

```ts
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,

```ts
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?](https://melvingeorge.me/blog/make-simple-get-request-api-endpoint-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,

```ts
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,

```ts
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,

```ts
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,

```bash
Hello John
```

We have successfully got all the query parameters from a `GET` request in Nestjs. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/get-all-query-parameters-in-get-request-nestjs-41v04r?file=/src/app.controller.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---