Angular Interceptors

In this tutorial, you will learn about Angular interceptors.

An Angular Interceptor is used to modify HTTP requests and responses globally.

Note: Angular supports multiple interceptors.

What is the use of Angular Interceptors?

There are lots of uses of Angular Interceptors

1. Angular Interceptors are used for HTTP requests and responses.

2. Angular Interceptors are used for handling errors globally.

3. Angular Interceptors are used to modify request headers.

4. Angular Interceptors are used to add authentication tokens.

Creating an Angular Interceptor

1. Generate Interceptor

You can generate an Interceptor from the command below


ng generate interceptor auth

2. Implement HttpInterceptor

Now, you will implement HttpInterceptor through below command.


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

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {

    const token = 'my-auth-token';

    const authReq = req.clone({
      setHeaders: {
        Authorization: `Bearer ${token}`
      }
    });

    return next.handle(authReq);
  }
}

}

3. Register Interceptor

Now, you will register AuthInterceptor


providers: [
  {
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptor,
    multi: true
  }
]

Note: multi: true is mandatory, otherwise Angular will override other interceptors.

Interceptor vs Service

Interceptor Service
Interceptor runs globally. Service runs when explicitly called.
Interceptor handles HTTP requests and responses. Service handles business logic.
Interceptor is used for cross-cutting concerns. Service is used for feature-specific logic.

Angular Interceptors – Interview Questions

Q 1: What are Angular Interceptors?

Ans: Angular Interceptors are services that intercept and modify HTTP requests and responses globally. They are commonly used for tasks like authentication, logging, error handling, and adding headers to API requests.

Q 2: Why do we use HTTP Interceptors in Angular?

Ans: Interceptors help centralize request and response handling. Instead of writing duplicate logic in multiple services, developers can manage authentication tokens, error handling, and request transformations in one place

Q 3: How do you create an Angular Interceptor?

Ans: You create an interceptor by implementing the HttpInterceptor interface and defining the intercept() method. Then, register it in the provider section using HTTP_INTERCEPTORS.

Q 4: Can multiple interceptors be used in Angular?

Ans: Yes, Angular supports multiple interceptors. They are executed in the order they are registered and process both outgoing requests and incoming responses.

Q 5: What is the purpose of the next.handle() method?

Ans: next.handle() passes the request to the next interceptor or the backend server. Without calling it, the request will not continue.