Sub topic 12
How httpResource builds on HttpClient and interceptors
This chapter assumes you can write a basic httpResource, know how provideHttpClient is set up, and have used functional HTTP interceptors. You will see how a resource sends its requests and what that means for cross-cutting concerns such as headers, logging and testing.
httpResource is a layer over HttpClient
httpResource does not talk to the network itself. When the request function returns a URL or request object, the resource passes it to the application's HttpClient. That is why the setup chapter required provideHttpClient(): without it, the resource has nothing to send requests with.
The resource adds three things on top of HttpClient:
- It tracks the request in signals (
value,status,error,isLoading). - It re-runs the request when signals read by the request function change.
- It unsubscribes from an outdated request when a newer one replaces it.
Everything else, including serialization, headers and the HTTP backend, belongs to HttpClient.
Interceptors apply automatically
Because the request goes through HttpClient, every interceptor registered with withInterceptors or withInterceptorsFromDi runs for it. You do not configure anything on the resource.
import { ApplicationConfig } from '@angular/core';
import {
HttpInterceptorFn,
provideHttpClient,
withInterceptors,
} from '@angular/common/http';
export const clientHeaderInterceptor: HttpInterceptorFn = (req, next) =>
next(req.clone({ setHeaders: { 'X-Client': 'web' } }));
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient(withInterceptors([clientHeaderInterceptor]))],
};Every httpResource in the application now sends X-Client: web. This is the right place for authentication headers, error logging, retry policies and base URL rewriting. Do not repeat them in each request object.
Passing context to interceptors
Sometimes one resource needs different interceptor behavior. The request object accepts an HttpContext, the same mechanism HttpClient uses. Define a token, read it in the interceptor, and set it on the request.
import { HttpContext, HttpContextToken, HttpInterceptorFn } from '@angular/common/http';
import { httpResource } from '@angular/common/http';
export const SKIP_CLIENT_HEADER = new HttpContextToken<boolean>(() => false);
export const clientHeaderInterceptor: HttpInterceptorFn = (req, next) =>
req.context.get(SKIP_CLIENT_HEADER)
? next(req)
: next(req.clone({ setHeaders: { 'X-Client': 'web' } }));
export function publicStatus() {
return httpResource<{ ok: boolean }>(() => ({
url: '/api/status',
context: new HttpContext().set(SKIP_CLIENT_HEADER, true),
}));
}What differs from a direct HttpClient call
The shared pipeline hides a few differences that matter in practice.
| Aspect | `HttpClient` | `httpResource` |
|---|---|---|
| When the request is sent | When you subscribe | When Angular runs the resource's reactive update, not synchronously at declaration |
| Result | Observable you manage | Signals the resource manages |
| Outdated requests | You cancel them, for example with `switchMap` | Cancelled by the resource |
| Methods | All | Intended for reads |
Interceptors that count in-flight requests or show a global spinner will see cancelled requests too. Handle cancellation in the interceptor, not by assuming every request completes.
Testing follows from the same design
Since requests go through HttpClient, the standard testing tools work. Provide provideHttpClient() and provideHttpClientTesting(), then use HttpTestingController to match and flush the request. Interceptors run in tests too, so assert on headers added by them if they matter.
Exercise
You registered an interceptor that adds an Authorization header with withInterceptors. A teammate asks whether each httpResource must be configured to use it. What do you answer, and how would you exclude one public resource?
Show answer
No configuration is needed, because httpResource sends its requests through HttpClient, so all registered interceptors run. To exclude one resource, define an HttpContextToken, have the interceptor skip adding the header when the token is true, and set the token with an HttpContext in that resource's request object.