How to skip http interceptor in Angular App
Askar Musthaffa
Posted on September 5, 2022
Angular Http Interceptor is used in many situations, such as adding header tokens, handling response errors, etc., but sometimes if you want to skip the interceptor layer intercepting request , I will expalin in this post how to deal with it using following steps.
Code
HttpBackend
Interceptors sit between the HttpClient
and the HttpBackend
interface.
When injected, HttpBackend
dispatches requests directly to the backend, without going through the interceptor chain.
HttpClient Code
@Injectable()
export class HttpClient {
constructor(private handler: HttpHandler) {}
...
}
HttpBackend
interface
abstract class HttpBackend implements HttpHandler {
abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>>
}
Sample Code
import {HttpBackend, HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
({
providedIn: 'root'
})
export class SkipInterceptorService {
private httpClient: HttpClient;
constructor(private handler: HttpBackend) {
this.httpClient = new HttpClient(handler);
}
}
Create one manual HttpClient
, and then httpClient
this will not pass through the Interceptor layer, it's that simple
References
Angular API - HttpBackend
Bypass Angular Interceptors with Request Metadata
Posted on September 5, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.