Concept of Using HTTP Interceptor in Angular 2

May 5th, 2017 . 2 minutes read
Blog featured image

Anyone who has created end-to-end applications most certainly had to deal with an HTTP interceptor in angular 2.

Interceptor is a middleware and a very useful concept for handling web APIs like add a token to the header, handle different kinds of errors, redirect the specific response, start and stop loading spinner, etc.
We can simply use an interceptor in angular 2 apps, for which all we need is to extend the HTTP class and that’s it.

Create a service class and extends HTTP class, as shown in the below example-

After that, you have to declare it as a provider in app.module.ts. Check the below code-

Now, every time you make an HTTP call, it logs the console before calling the base HTTP request. I have placed a console where you can place any logic you want.

You can check the demo for use of an HTTP interceptor here

If you have any queries and suggestions about HTTP interceptor using, then reach out to us in the comment box. We are the best web development providers, contact us for your project.

Share this post with your friends if you enjoyed reading it.

Author: monika