@Headers()

Extracts request headers or a single header into a handler parameter.

Since NestJS 10/11 Spec ↗

Syntax

@Headers(name?: string)

Parameters

NameTypeRequiredDescription
name string No Optional header name to extract a single value.

Returns

ParameterDecorator — A decorator that binds headers to the parameter.

Examples

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

@Controller('cats')
export class CatsController {
  @Get()
  find(@Headers('authorization') auth: string) {
    return auth?.startsWith('Bearer ');
  }

  @Get('all')
  all(@Headers() headers: Record<string, string>) {
    return headers;
  }
}

Notes

Header names are case-insensitive. Without an argument the full headers object is returned. For typed authentication prefer a Guard with a Passport strategy instead of reading headers manually.