With the introduction of a new HttpClient in Angular 4.3, an old feature of Angular.js was re-introduced; HttpInterceptors. Interceptors are sitting between the application and the backend and allow you to transform a request coming from the application before it is actually submitted to the backend. And of course you when a response arrivers from the backend an interceptor can transform it before delivering it to your application logic.
This allows us to simplify the interaction with the backend in our Angular app and hide most of the shared logic inside an interceptor.
Let’s create a simple example that injects an OAuth token in our requests:
To be able to use the interceptor, you’ll have to register it: