What is HTTP Interceptor in Angular ?

HTTP Interceptor in Angular is always a hot topic among interviewers. Angular Interceptors have been used to pre-process and post-process the HTTP request before sending and after getting a response from the server. Interceptors can be used in two different phases in a life cycle of an HTTP request to a server, which are:

We can also understand it as a simple way provided by the Angular framework to intercept and modify the application’s HTTP requests globally before they are sent to the server. That really comes in handy, allowing us to configure authentication tokens, add logs of the requests, add custom headers that out the application may need and much more.

Interceptors can be used to change the request configuration of HTTP call. The most common use case for this is to add an Access Token to be sent to the server with each HTTP request so that it can be validated at the server end.

After getting the response from the server, it can be used to change the response before it is being passed to the code block from where the HTTP call was made. The most common use case for this is to Handle Global Error Handling across the app.

Leave a Comment