Skip to content
the poor coder

angular interceptor

Poor Man
Published
Reading time
2 min read
On this page

What is Angular Interceptor?#

Angular Interceptors are a powerful feature of Angular that allow you to intercept HTTP requests and responses. They provide a way to modify requests and responses, add custom headers, handle errors, or perform other operations based on the request or response.

How to create an Angular Interceptor?#

To create an Angular Interceptor, you need to create a class that implements the HttpInterceptor interface. The interface requires you to implement a single method called intercept, which takes two arguments: a HttpRequest object and a HttpHandler object.

Text
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable()
export class CustomInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // your code here
    return next.handle(req);
  }
}

You can add your custom logic inside the intercept method. If you want to pass the request to the next interceptor or to the HttpClient, you need to call the next.handle(req) method.

How to use an Angular Interceptor?#

To use an Angular Interceptor, you need to add it to the providers array in your module. You can do this in the @NgModule decorator of your module:

Text
@NgModule({
  declarations: [ ... ],
  imports: [ ... ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: CustomInterceptor,
      multi: true
    }
  ],
  bootstrap: [ ... ]
})
export class AppModule { }

In the above example, we added the CustomInterceptor to the providers array using the HTTP_INTERCEPTORS token. We set the multi property to true because we want to add multiple interceptors.

Multiple Interceptors in Angular#

You can add multiple interceptors in Angular. Angular will call each interceptor in the order they are defined in the providers array. You can also define the order of your interceptors by setting a provide property on them:

Text
@Injectable({
  providedIn: 'root',
  provide: HTTP_INTERCEPTORS,
  useClass: CustomInterceptor,
  multi: true
})
export class CustomInterceptor1 implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // your code here
    return next.handle(req);
  }
}

@Injectable({
  providedIn: 'root',
  provide: HTTP_INTERCEPTORS,
  useClass: CustomInterceptor2,
  multi: true
})
export class CustomInterceptor2 implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // your code here
    return next.handle(req);
  }
}

In the above example, we added two interceptors to the providers array with the same HTTP_INTERCEPTORS token but different provide properties. Angular will call CustomInterceptor1 before CustomInterceptor2.

Angular Interceptors are a powerful feature that makes it easy to handle HTTP requests and responses in your Angular application. They are easy to use and can save you a lot of time and effort.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC