Operations of HTTP Interceptor
Angular interceptors allow us to do a lot of different things.
Modify HTTP Headers
It can change the data in HTTP request headers.
In higher version of Angular use HttpInterceptorFn
Modifying The Request Body
Request body, headers, and response data formats can all be changed by interceptors to customise data transformation for both requests and responses before the application processes them.
In higher version of Angular use HttpInterceptorFn
Cancel The Current Request
We can also terminate the current request by restoring the EMPTY observable.
In higher version of Angular use HttpInterceptorFn
Set Authentication Token
To enable authentication in a real-time environment, it could be required to include a JSON token or an API key in the header.
In higher version of Angular use HttpInterceptorFn
Modifying The Request URL
interceptors can be used to dynamically change URLs, such as adding versioning to APIs or rerouting traffic to alternative base URLs depending on the environment (development vs. production)
In higher version of Angular use HttpInterceptorFn
Modify The HTTP Response
It is also possible to modify the HTTP response. Lets convert XML data to JSON
In higher version of Angular use HttpInterceptorFn
Error Handling
Errors that occur during the HTTP request process can also be handled by interceptors.
Errors like network problems or HTTP errors (404, 500, etc.) are intercepted and handled inside the pipe's error callback.
We can do custom error handling logic, log the error information, retry the request, or modify error answers.
In higher version of Angular use HttpInterceptorFn
Caching
Create an Angular service and register outbound HTTP requests for caching in order to cache requests
Use above service
See Also: Angular Interceptors