How to set or send a static response header for a GET request in Nestjs?
Tags: resource
Published: 2022-08-13 19:06:40

---
  title: How to set or send a static response header for a GET request in Nestjs?
  published: true
  tags: resource
  date: Sun Aug 14 2022 00:36:40 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/set-or-sent-static-response-header-for-get-request
  cover_image: https://melvingeorge.me/_next/static/images/main-bd22974b9dc68903987a458905c1519c.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/set-or-sent-static-response-header-for-get-request)
  
To set or send a static header for a `GET` request in Nestjs, we can use the `@Header()` decorator function from the `@nestjs/common` module before the `Controller` class method that handles that `GET` request.

### TL;DR

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

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,

```ts
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?](https://melvingeorge.me/blog/make-simple-get-request-api-endpoint-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,

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

Now if we send a `GET` 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 `GET` request in Nestjs. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/set-or-sent-static-response-header-for-get-request-gbldg9?file=/src/greet/greet.controller.ts).

To see the header in the response, go to this [Hoppscotch link](https://hopp.sh/r/mzBKGXuA0TwU) and make a request.

That's all 😃!

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

---