Sub topic 2
Setting Up HttpClient with provideHttpClient
This chapter assumes you know how a standalone Angular application starts with bootstrapApplication, and how dependency injection supplies services through providers. HttpClient is a service, so before any component or service can inject it, the application needs a provider for it. In a standalone application, that provider comes from the provideHttpClient() function.
Where the provider goes
A standalone app usually keeps its application-wide providers in an ApplicationConfig object. This object lives in app.config.ts and gets passed to bootstrapApplication in main.ts. HttpClient belongs in that list.
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient, withFetch } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(withFetch())
]
};// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig)
.catch(err => console.error(err));Any class created by the root injector can now call inject(HttpClient). The next chapter does this inside a data service.
Feature functions
provideHttpClient takes optional feature functions as arguments. Each one changes how the client behaves. You pass all of them in a single call.
| Feature | What it does |
|---|---|
| `withFetch()` | Sends requests with the browser `fetch` API instead of `XMLHttpRequest`. Recommended for server-side rendering. |
| `withInterceptors([...])` | Registers functional interceptors. A later chapter covers these. |
| `withInterceptorsFromDi()` | Enables older class-based interceptors registered with the `HTTP_INTERCEPTORS` token. |
| `withXsrfConfiguration({...})` | Changes the cookie and header names used for XSRF protection. |
| `withNoXsrfProtection()` | Turns off the built-in XSRF header. |
| `withJsonpSupport()` | Allows `http.jsonp()` calls for legacy cross-origin APIs. |
provideHttpClient(
withFetch(),
withXsrfConfiguration({ cookieName: 'XSRF-TOKEN', headerName: 'X-XSRF-TOKEN' })
)Moving off HttpClientModule
Older applications imported HttpClientModule into an NgModule. Angular has deprecated that module in favor of provideHttpClient(). If you are migrating, the swap is direct: delete the module import and add provideHttpClient(withInterceptorsFromDi()) so your existing class interceptors keep working. Once they are rewritten as functions, you can move to withInterceptors.
Some recent Angular versions can inject a default HttpClient with no explicit setup. Calling provideHttpClient() yourself is still how you add features, and it makes the configuration visible in one place.
Common errors
NullInjectorError: No provider for HttpClient!The provider is missing from the injector that created your service, or the test bed does not include it. AddprovideHttpClient()toappConfig, or to the test's providers.- Providing it twice. Calling
provideHttpClient()again in a lazy route'sproviderscreates a separate HttpClient for that route. Interceptors registered at the root will not run for requests from that route unless you passwithRequestsMadeViaParent(). Usually you want one root call. - Multiple calls for features. Splitting features across two
provideHttpClientcalls in the same injector does not merge them. Put every feature in one call. - Importing from the wrong package. Everything here comes from
@angular/common/http, not@angular/core.
Exercise
Your app has provideHttpClient(withInterceptors([authInterceptor])) in appConfig. A lazily loaded admin route also lists provideHttpClient() in its route providers. Requests from admin components arrive at the server without the Authorization header. Why, and what are two ways to fix it?
Show answer
The route-level call creates a new HttpClient in the route's injector, and that client does not use the root interceptors. Fix it by removing the route-level provideHttpClient() so the root client is used, or by changing it to provideHttpClient(withRequestsMadeViaParent()) so its requests also pass through the parent's interceptor chain.
Exercise
You are migrating an NgModule app that uses HttpClientModule and two class-based interceptors registered with HTTP_INTERCEPTORS. What provider call keeps the interceptors working after you switch to standalone bootstrapping?
Show answer
provideHttpClient(withInterceptorsFromDi()), with the existing HTTP_INTERCEPTORS providers left in place in the application providers.