DEV Community

Cover image for How to set or send a static response header for a POST request in Nestjs?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

1 1

How to set or send a static response header for a POST request in Nestjs?

Originally posted here!

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

TL;DR

// import `@Header()` decorator function from the `@nestjs/common` module
import { Controller, Post, Header } 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. use the @Header() decorator function and
  // pass the header name as the first argument
  // and the header value as the second argument
  @Post()
  @Header("x-app-name", "MyApp")
  sayHello() {
    return `Hello World`;
  }
}
Enter fullscreen mode Exit fullscreen mode

For example, let's say we have an API endpoint called /greet, and requesting it will give us a response of Hello World.

It can be done 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`;
  }
}
Enter fullscreen mode Exit fullscreen mode

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 if we need to send a custom header called x-app-name with the value of My App, we can use the @Header() decorator function and use just above the sayHello() method.

The @Header() decorator function accepts 2 arguments:

  • the first argument should be the header name
  • and the second argument should be the header value

In our case it will look like this,

// import `@Header()` decorator function from the `@nestjs/common` module
import { Controller, Post, Header } 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. use the @Header() decorator function and
  // pass the header name as the first argument
  // and the header value as the second argument
  @Post()
  @Header("x-app-name", "MyApp")
  sayHello() {
    return `Hello World`;
  }
}
Enter fullscreen mode Exit fullscreen mode

Now if we send a POST request to the /greet API endpoint, we will get a response of Hello World along with the header of x-app-name having the value of MyApp.

We have successfully set a static response header for a POST request in Nestjs. Yay 🥳!

See the above code live in codesandbox.

To see the header in the response, go to this Hoppscotch link and make a request.

That's all 😃!

Feel free to share if you found this helpful 😃.


Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay