DEV Community

Askar Musthaffa
Askar Musthaffa

Posted on

How to skip http interceptor in Angular App

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 HttpClientand the HttpBackendinterface.

When injected, HttpBackenddispatches requests directly to the backend, without going through the interceptor chain.

HttpClient Code

@Injectable()
export class HttpClient {
  constructor(private handler: HttpHandler) {}
    ...
}


Enter fullscreen mode Exit fullscreen mode

HttpBackendinterface

abstract class HttpBackend implements HttpHandler {
  abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>>
}
Enter fullscreen mode Exit fullscreen mode

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);
  }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)