From Http wrappers to Http Interceptors in Angular

Shreevardhan
Docon
Published in
1 min readAug 22, 2018

Prior to Angular 4.3, if you wanted to transform every HTTP request before initiating it (to perform implicit tasks, from authentication to logging), the best way was to wrap the Http service with another injectable, for instance

Now that Http module is obsolete and replaced with a smarter HttpClient module and of course, rxjs has changed too. Angular has introduced a smarter way to transform each request, i.e. via interceptors. Simply putting, you just need to inherit HttpInterceptor and provide it in your root module. A typical implementation looks like this

and it is provided in the root module

providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: RestInterceptor,
multi: true
},
. . .
]

With this, all requests going through HttpClient module will call the intercept method and voila, done. For more information on interceptors, explore Angular - HttpClient.

--

--