Recently I have been using Interceptors with Angular HttpClient.
I add headers corresponding to some HTTP GET methods and for some I do not need those headers.
How can I tell my interceptor to conditionally add interceptors to only those methods? I can even split up services like one service for headers and one without headers or one for different headers and one for different.
NgModule providers
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},{
provide: HTTP_INTERCEPTORS,
useClass: AngularInterceptor,
multi: true,
}
MyInterceptors
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({headers: req.headers.set('X-Auth-Token', "-------------------------")});
return next.handle(authReq);
}
}
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do(event => {}, err => {
if(err instanceof HttpErrorResponse){
console.log("Error Caught By Interceptor");
//Observable.throw(err);
}
});
}
}
intercept
method is called for every request, by design. – Strepphon