Sub topic 2
Setting up HttpClient
httpResource sends its requests through Angular's HttpClient. Before you declare any resource, the application has to provide HttpClient. This chapter shows how to do that and what goes wrong if you skip it.
This chapter assumes you know TypeScript, Angular components and templates, and signals. It also assumes you know basic HTTP concepts such as methods, query parameters and headers. You also need an Angular project that uses standalone components and an ApplicationConfig, the default in current Angular versions.
Why setup comes first
HttpClient is not available automatically. Angular only creates it when you register its providers. An httpResource asks the dependency injection system for HttpClient. If nothing provides it, the lookup fails and the resource does not work.
Providing HttpClient
Add provideHttpClient() to the providers array in your application config:
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient()],
};Then pass the config when you bootstrap the application:
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));Both httpResource and any service that injects HttpClient now share the same instance. You only need to do this once for the whole application.
Optional features
provideHttpClient() accepts feature functions as arguments. These are the ones you are most likely to use:
| Feature | What it does |
|---|---|
| `withInterceptors([...])` | Registers functional interceptors that can change every request and response. |
| `withFetch()` | Uses the browser `fetch` API instead of `XMLHttpRequest`. |
| `withInterceptorsFromDi()` | Keeps class-based interceptors that are registered through dependency injection. |
For example, this config registers one functional interceptor that adds a header to every request:
import { ApplicationConfig } from '@angular/core';
import {
HttpInterceptorFn,
provideHttpClient,
withInterceptors,
} from '@angular/common/http';
const addClientHeader: HttpInterceptorFn = (req, next) =>
next(req.clone({ setHeaders: { 'X-Client': 'web' } }));
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient(withInterceptors([addClientHeader]))],
};Because httpResource uses HttpClient, its requests pass through these interceptors too. A later chapter covers this in more detail.
Common errors
NullInjectorError: No provider for HttpClient. This meansprovideHttpClient()is missing from the providers that the running code can see. Check that you passedappConfigtobootstrapApplication.- Interceptors seem to be ignored. Make sure you registered them through a
with...feature ofprovideHttpClient(). Declaring an interceptor function without registering it does nothing. - Failures only in unit tests. A test has its own injector, so it does not use your app config. The test must provide
HttpClientitself. A later chapter shows how.
Exercise
A component declares user = httpResource<User>(() => '/api/users/1'). When you open the page, the console shows "NullInjectorError: No provider for HttpClient". What is the most likely cause, and how do you fix it?
Show answer
The application never registered HttpClient, and httpResource needs it internally. Add provideHttpClient() to the providers array of the ApplicationConfig, and make sure that config is passed to bootstrapApplication.
Exercise
You want every request made by an httpResource to carry an X-Client header. Where do you configure this, and why does it work for httpResource?
Show answer
Write a functional interceptor and register it with provideHttpClient(withInterceptors([...])) in the root providers. It works because httpResource sends its requests through HttpClient, so the interceptor chain applies to them.