I did not find in the documentation how to extend HttpClient or how to specify differents interceptors for some of my HTTP requests. In documentation, they explain how to set interceptors in HTTP_INTERCEPTORS
but every HTTP requests are intercepted.
I need to have customizes HttpClient implementations to set specifics Headers, endpoints or response interceptors (I don't want to use Restangular, I would prefer to use the built-in angular HttpClient implementation).
Authorization: Bearer
... and API-KEY) and a specific response interceptor to catch all HTTP 401 responses.I know this is possible with Restangular but I prefer to use HttpClient
. How is it possible ?
I found this article but, this is for Http
not for the new HttpClient
implementation of angular.
The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http. They expect to deprecate Http in Angular 5 and remove it in a later version.
The basic way to handle errors in Angular is to use Angular's HttpClient service along with RxJS operators throwError and catchError. The HTTP request is made, and it returns the data with a response if anything wrong happens then it returns an error object with an error status code.
Why Do We Need HttpClient? The front-end of applications communicate with back-end services to get or send the data over HTTP protocol using either XMLHttpRequest interface or fetch API . This communication is done in Angular with the help of HttpClient .
catch( (error: Response) => { return Observable. throw(error); } );
Instead of extending HttpClient, I would take one of three approaches:
next.handle(req)
without doing anything else, depending on the URLor
or
My gut would be to go with #1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With