Sub topic 5
Registering interceptors with withInterceptors
This chapter assumes you can write a functional interceptor, which is a function of type HttpInterceptorFn that takes req and next. It also assumes you know Angular providers. An interceptor does nothing until you register it with HttpClient.
Where registration happens
Interceptors belong to the HttpClient configuration, so you register them where you call provideHttpClient. In a standalone application, that is the providers array passed to bootstrapApplication. You wrap your interceptor functions in the withInterceptors feature:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { AppComponent } from './app/app.component';
import { authInterceptor } from './app/auth.interceptor';
import { loggingInterceptor } from './app/logging.interceptor';
bootstrapApplication(AppComponent, {
providers: [
provideHttpClient(withInterceptors([authInterceptor, loggingInterceptor])),
],
});withInterceptors takes an array of functions, not classes and not calls. Write authInterceptor, not authInterceptor(). Each function must match HttpInterceptorFn.
If your app uses app.config.ts, put the same provideHttpClient(...) call in the providers array of the ApplicationConfig object. The behavior is the same.
Order of execution
The array defines a chain. A request travels through the interceptors from first to last and then reaches the backend. The response travels back through them in reverse order.
With this array, authInterceptor runs first on the way out. That means loggingInterceptor sees the request after the Authorization header was added. On the way back, loggingInterceptor sees the response first.
Order matters whenever one interceptor depends on another's work. If you swap the two, the logger would record the request without the header. Put interceptors that change requests before the ones that need to see the final request.
Rules to remember
- Call
provideHttpClientonce for the application. If a second call appears in a lazy route or componentproviders, that injector gets its ownHttpClientsetup. Interceptors registered at the root do not automatically apply there. - Each
withInterceptorscall inside oneprovideHttpClientbelongs to that configuration. Put all functional interceptors in a single array so the order is visible in one place. - Interceptors only affect requests made through
HttpClient. A call tofetchorXMLHttpRequestbypasses them. - Every interceptor must call
next(req), or return another observable. If it does not, the request is never sent and subscribers never get a result.
Common errors
Passing the wrong thing to withInterceptors is the usual mistake. A class decorated with @Injectable fails to type-check here, because the feature expects functions. Class-based interceptors use withInterceptorsFromDi instead, which a later chapter covers.
Another frequent cause of "my interceptor never runs" is a missing provideHttpClient, or a service that creates its own HttpClient through a different provider setup. Check that the injector your service uses is the one where you registered the interceptors.
Exercise
You register provideHttpClient(withInterceptors([a, b, c])). In which order do the interceptors see the request, and in which order do they see the response?
Show answer
The request passes through a, then b, then c, and then goes to the backend. The response comes back through c, then b, then a.
Exercise
An interceptor is registered correctly, but a request made with fetch('/api/data') does not trigger it. Why?
Show answer
Interceptors are part of HttpClient. fetch does not use HttpClient, so its requests never enter the interceptor chain.