Jackie
Jackie

Reputation: 23577

How do I get a request header using NestJS?

I have a simple app that returns Something: Value as a header. I currently have the following as a controller...

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

@Controller("health")
export class HealthController {
  @Get()
  @Header("content-type", "application/json")
  checkHealth(): unknown {
    return {
      test: "This is the test",
    };
  }
}

In express I would expect to be able to do something like req.headers but I am not sure how to do that in nestjs.

Upvotes: 4

Views: 17372

Answers (3)

Jay McDoniel
Jay McDoniel

Reputation: 70412

You have two options to get the headers. You can use @Req() req in the route handler and then get req.headers, or you can use @Headers() headers in the route handler and then headers is the same as req.headers

import { Controller, Get, Header, Headers } from "@nestjs/common";
import { Request } from 'express'; 
// OR
import { FastifyRequest } from 'fastify'

@Controller("health")
export class HealthController {
  @Get()
  @Header("content-type", "application/json")
  checkHealth(@Req() req: Request | FastifyRequest): unknown { // remove which isn't used here
    console.log(req.headers);
    return {
      test: "This is the test",
    };
  }
}

Or

import { Controller, Get, Header, Headers } from "@nestjs/common";

@Controller("health")
export class HealthController {
  @Get()
  @Header("content-type", "application/json")
  checkHealth(@Headers() headers: Record<string, string>): unknown {
    console.log(headers);
    return {
      test: "This is the test",
    };
  }
}

Upvotes: 3

KCP
KCP

Reputation: 33

You can create a decorator to pull out the headers from the request context.

export const RequestHeaders = createParamDecorator(
  async (ctx: ExecutionContext) => {
    // extract headers
    const headers = ctx.switchToHttp().getRequest().headers;
    return headers;
  },

and when used in a controller:

@Post('/route')
    async controllerMethod(@RequestHeaders() headers) {
    console.log(headers)
    // returns all headers in incoming request
    }

shamelessly stolen from this answer, thank you to whoever wrote it: https://github.com/nestjs/nest/issues/4798#issuecomment-706176390

Upvotes: 0

ruciu
ruciu

Reputation: 722

You should pass Headers from @nestjs/common as an argument to function:

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

@Controller("health")
export class HealthController {
  @Get()
  checkHealth(@Headers() headers: Record < string, string > ) {
    return {
      test: "This is the test",
    };
  }
}

If you need just one header you can pass it's name to header like this: @Headers('content-type') headers: string.

Alternatively if you want access to express req object you can also pass it to your controller

import { Controller, Get, Req } from "@nestjs/common";
import { Request } from 'express';

@Controller("health")
export class HealthController {
  @Get()
  checkHealth(@Req() req: Request) {
    return {
      test: "This is the test",
    };
  }
}

Upvotes: 11

Related Questions