How to set or send a static response status code for a GET request in Nestjs?
Tags: resource
Published: 2022-07-08 00:00:00

---
  title: How to set or send a static response status code for a GET request in Nestjs?
  published: true
  tags: Resource
  date: Fri Jul 08 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/set-or-sent-static-response-status-code-for-get-request-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-7d1933a926ae7cc596e37e3efe245078.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/set-or-sent-static-response-status-code-for-get-request-nestjs)
  
To set or send a static response status code for a `GET` request in Nestjs, we can use the `@HttpCode()` decorator function from the `@nestjs/common` module before the `Controller` class method that handles that `GET` request.

### TL;DR

```ts
// import `HttpCode` decoration function from the `@nestjs/common` module
import { Controller, Get, HttpCode } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  // 2. Using the @HttpCode() decoration and passing
  // the `204` status code as its argument to set
  // the static status code for this `GET` request
  @Get()
  @HttpCode(204)
  sayHello() {
    return `Hello World`;
  }
}
```

For example, let's say we have a `GET` API endpoint called `/greet` and on requesting to that we need to get a response with a static status code of [`204` (No Content)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204).

To do that first we can make a `Controller` class and define the `GET` request endpoint like this,

```ts
import { Controller, Get } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
```

**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 also import the `@HttpCode()` decorator function from the `@nestjs/common` module and use it just above the `sayHello()` method.

After that we need to pass the static status code, in our case `204` (`number` type) as the argument to the `@HttpCode()` decorator function. This status code will then be send with the response.

It can be done like this,

```ts
// import `HttpCode` decoration function from the `@nestjs/common` module
import { Controller, Get, HttpCode } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  // 2. Using the @HttpCode() decoration and passing
  // the `204` status code as its argument to set
  // the static status code for this `GET` request
  @Get()
  @HttpCode(204)
  sayHello() {
    return `Hello World`;
  }
}
```

We have successfully set a static response status code for a `GET` request in Nestjs. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/set-static-response-status-code-for-get-request-nestjs-85qows?file=/src/greet/greet.controller.ts).

Visit this [Hoppscotch URL](https://hopp.sh/r/midEw0AhcPX0) to see if the `204` status code is returned with the response.

That's all 😃!

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

---