Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS: Logging the request/response from HttpService calls?

I was wondering if its possible to log the requests, response and errors using the HttpService from the HttpModule.

I used to use Interceptors from AXIOS, HttpService wraps axios but I can't seem to add any interceptors here, there doesn't seem to be a place in

HttpModule.register(...)

Then I thought that NestJS comes with its own interceptors and wondered if its possible to use NestJS interceptors.

I wouldn't want to apply the interceptor over a controller, service but apply it to the HttpService?

Any ideas, a little lost how to do this in the nestjs way.

Thanks in advance

like image 315
Ian Gregson Avatar asked Mar 13 '26 07:03

Ian Gregson


1 Answers

The HttpService exposes its axios instance directly via get axiosRef(). With it, you can add an axios interceptor:

this.httpService.axiosRef.interceptors.request.use(config => console.log(config));

You can for example do that in the onModuleInit() of your AppModule.

like image 109
Kim Kern Avatar answered Mar 14 '26 21:03

Kim Kern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!