How to set or sent a static response status code for a POST request in Nestjs?
Tags: resource
Published: 2022-08-13 19:07:10

---
  title: How to set or sent a static response status code for a POST request in Nestjs?
  published: true
  tags: resource
  date: Sun Aug 14 2022 00:37:10 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/set-or-sent-static-response-status-code-for-post-request-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-961ad6a01a94d76b3fa67f699c8fc1e1.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/set-or-sent-static-response-status-code-for-post-request-nestjs)
  
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

```ts
// 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)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204).

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

```ts
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?](https://melvingeorge.me/blog/make-simple-post-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 sent with the response.

It can be done like this,

```ts
// 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](https://codesandbox.io/s/set-or-sent-static-response-status-code-for-post-request-nestjs-sbrj51?file=/src/greet/greet.controller.ts).

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

That's all 😃!

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

---