How to set or send a static response status code for a GET request in Nestjs?

July 8, 2022 - 3 min read

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

// 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).

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

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?.

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,

// 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.

Visit this Hoppscotch URL to see if the 204 status code is returned with the response.

That's all 😃!

Feel free to share if you found this helpful 😃.