How to set or sent a static response status code for a POST request in Nestjs?

July 9, 2022 - 3 min read

To set or sent a static response status code for a POST request in Nestjs, we can use the @HttpCode() decorator function from the @nestjs/common module before the Controller class method that handles that POST request.

TL;DR

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

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Post() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `POST` 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 `POST` request
  @Post()
  @HttpCode(204)
  sayHello() {
    return `Hello World`;
  }
}

For example, let's say we have a POST 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 POST request endpoint like this,

import { Controller, Post } from "@nestjs/common";

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

To know more about creating a POST request in Nestjs, see the blog on How to make a simple POST 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 sent with the response.

It can be done like this,

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

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Post() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `POST` 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 `POST` request
  @Post()
  @HttpCode(204)
  sayHello() {
    return `Hello World`;
  }
}

We have successfully set a static response status code for a POST 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 😃.