Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!