We can create by implementing the HttpInterceptor interface from @angular/common/http package. We register it to be attached to the HTTP request pipeline by adding it to the providers array in the NgModule. We can also enable DI into the interceptor by registering it as a service.
@Injectable({
providedIn: "root"
})
export class PostsInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler)
: Observable<HttpEvent<any>> {
// fiddling happens here
return next.handle(req);
}
}
// NgModule
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: PostsInterceptor,
multi: true
}]