So I'm likely missing something or doing something wrong. 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.
I've tried using Interceptors to no avail, the incoming http requests get intercepted but not the outgoing. Any suggestions or help would be greatly appreciated.
In order to set up the interceptor, we use the @UseInterceptors() decorator imported from the @nestjs/common package. Like pipes and guards, interceptors can be controller-scoped, method-scoped, or global-scoped. Hint The @UseInterceptors() decorator is imported from the @nestjs/common package.
In this pa- per we present Interceptor, a middleware-level application segregation and scheduling system that is able to strictly en- force quantitative limitations on node resource usage and, at same time, to make P2P applications achieve satisfactory performance even in face of these limitations.
Interceptors are a mechanism for users to modify inbound and outbound SDK calls. Interceptors are commonly used to add tracing and authorization to the scheduling and execution of Workflows and Activities. You can compare these to "middleware" in other frameworks.
Interceptors are code blocks that you can use to preprocess or post-process HTTP calls, helping with global error handling, authentication, logging, and more.
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;
}),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With