DEV Community

Askar Musthaffa
Askar Musthaffa

Posted on

2 1

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

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay