nestjs intercept and modify outgoing http request

Let's first deal with

I've tried using Interceptors to no avail, the incoming http requests get intercepted but not the outgoing.

According to the documentation https://docs.nestjs.com/interceptors it should be totally possible to intercept the response.

@Injectable()
export class TransformHeadersInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    call$: Observable<any>,
  ): Observable<any> {
    // Get request headers, e.g.
    const userAgent = context.switchToHttp().getRequest().headers['user-agent'];

    // Not sure if headers are writeable like this, give it a try
    context.switchToHttp().getResponse().headers['x-api-key'] = 'pretty secure';

    return call$;
  }
}

If you want to manipulate headers based on the the response data. You could tap into the data like so:

return call$.pipe(map(data => {
    // Your code here
    return data;
}));

I have some thoughts on:

I have a NestJS application that is trying to make an http request to an external API. I'd like to be able to intercept this outgoing request and modify headers on it before executing it.

So I think there two use cases. First, you have a set of default headers, that are assigned to the http client initially and send with each request. E.g.:

import { HTTP_TOKEN } from './constants';
import * as http from 'request-promise-native';

export const httpProviders: any = [
  {
    provide: HTTP_TOKEN,
    useFactory: () => {
      return http.defaults({
        headers: {
          'Accept': 'application/json',
          'Content-type': 'application/json',
          'User-agent': 'my--app',
        },
      });
    },
  },
];

And second, you create and assign headers per request. This is when you use interceptors. In the context of authentication, you could think about using a guard, like tano suggests in his answer.


I had a similar problem modifying / adding response headers. Following code worked for me:

@Injectable()
export class TransformHeadersInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    call$: Observable<any>,
  ): Observable<any> {

    return call$.pipe(
            map((data) => {
                // pipe call to add / modify header(s) after remote method
                let req = context.switchToHttp().getRequest();
                req.res.header('x-api-key', 'pretty secure');
                return data;
            }),
        );
  }
}