Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
How to add header to all responses in NestJS v8 + GraphQL?

I want to add custom header with value to all responses in NestJS framework (v8). I think the correct way is to use global interceptor, but I can't figure out how to do it.

I am adding my interceptor:

app.useGlobalInterceptors(new HeadersInterceptor());

and I found multiple approaches but none of them work. The most common looks like:

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, tap } from 'rxjs';

@Injectable()
export class HeadersInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      tap(() => {
        const res = context.switchToHttp().getResponse();

        res.setHeader('my-global-header', 'important-value');
      }),
    );
  }
}

but I'm getting an error:

res.setHeader is not a function

EDIT: Judging from the correct answer I should have mentioned that I'm also using GraphQL.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This depends on which context NestJS is executing. Here I have example for GraphQL and HTTP context.

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { Response } from 'express';
import { Observable } from 'rxjs';

@Injectable()
export class VersionHeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    // When the request is GraphQL
    if ((context.getType() as string) === 'graphql') {
      const gqlExecutionContext = GqlExecutionContext.create(context);
      const response: Response = gqlExecutionContext.getContext().res;
      response.setHeader('x-version', process.env.npm_package_version);
    }

    // When the request is HTTP
    if (context.getType() === 'http') {
      const http = context.switchToHttp();
      const response: Response = http.getResponse();
      response.setHeader('x-version', process.env.npm_package_version);
    }

    return next.handle();
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You code looks fine. What happens if you try to use Response interface from express?

import { Response } from 'express';

@Injectable()
export class HeadersInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const res: Response = context.switchToHttp().getResponse();

    res.setHeader('my-global-header', 'important-value');

    return next.handle();
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could simply use the set method on the response object

res.set('my-global-header', 'important-value')

I believe the setHeader is one of the options provided in express.static(). You could look up the docs on this

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda