How to Intercept HTTP Requests with the HttpInterceptor - Angular 6 | 7 - CodeWithSrini

CodeWithSrini
CodeWithSrini
37.5 هزار بار بازدید - 5 سال پیش - In this tutorial we will
In this tutorial we will look at how to use HTTP Interceptor in an Angular project.
HttpInterceptors is feature in Angular that provides a great way to intercept outgoing request or incoming responses.

When you make a request, you can use HTTP interceptors to intercept and modify the request as you like it. In other words you can mutate your outgoing requests.

Why do you need an Interceptors?

Simple, let’s say you are working on a project and you may have to prefix all your API calls with a server name, you can achieve that using interceptor.
Not just this.
You can think of many many uses of interceptors, to name a few…
1. Ensure the outgoing request is HTTPS instead of HTTP
2. Set an authorization header on each request
3. You can create a global error catch in case HTTP request fails, or
4. You can create a mock response for your application to test

Implementing an HTTP Interceptor is fairly straight forward, all you need to do is create a class with an injectable decorator that implements HttpInterceptor.
5 سال پیش در تاریخ 1398/04/07 منتشر شده است.
37,593 بـار بازدید شده
... بیشتر