DEV Community

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

Posted on • Edited on • Originally published at melvingeorge.me

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

Originally posted here!

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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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


Top comments (0)