DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Set Up HttpClient in Angular

Use provideHttpClient() in application providers, then add only the Angular HttpClient features your app needs. Testing uses a separate provider with an order-sensitive setup.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Configure Angular’s HTTP client with provideHttpClient() in your application providers. The right place depends on whether the app uses standalone bootstrap or NgModule bootstrap; tests use a separate testing provider. Angular’s current guide says HttpClient is available for injection by default starting in Angular v21, while provideHttpClient(...) remains the way to add features such as interceptors or customize behavior. See Angular’s HttpClient setup guide.

Choose the setup for your Angular app

First check the project’s Angular version and bootstrap style. For current standalone applications, configure the client in the application configuration. For an NgModule-based application, add the provider to the application module’s providers array. Both approaches use provideHttpClient().

Standalone application configuration

Import provideHttpClient from @angular/common/http and add it to the providers in app.config.ts:

import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';

export const appConfig: ApplicationConfig = {
  providers: [provideHttpClient()],
};

With that provider configured, injectable services and other classes can inject HttpClient from @angular/common/http and use it to make requests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NgModule application

For an application bootstrapped with NgModules, import provideHttpClient and include it in the application module’s providers array:

import { NgModule } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';

@NgModule({
  providers: [provideHttpClient()],
})
export class AppModule {}

In Angular v21 and later, the current guide says HttpClient is provided for injection by default. Use the provider helper when you need to configure client features. If you are updating a legacy application, check its Angular version and existing backend and interceptor setup before replacing HttpClientModule. Angular maps the older module to provideHttpClient(withInterceptorsFromDi(), withXhr()); that is not necessarily the configuration you want in a newer application.

Add only the HttpClient features you need

provideHttpClient(...) accepts optional features. A plain provideHttpClient() is the starting point; add configuration when your application has a specific requirement. The available options and their behavior are described in the setup guide.

  • withInterceptors([...]) registers functional interceptors.
  • withInterceptorsFromDi() enables class-based interceptors provided through dependency injection.
  • withRequestsMadeViaParent() forwards requests from a child injector’s client to the parent client.
  • withJsonpSupport() adds JSONP support. Angular advises preferring CORS where possible.
  • withXsrfConfiguration(...) customizes the built-in XSRF configuration.
  • withNoXsrfProtection() disables XSRF protection; do not use it casually.
  • withXhr() selects the XMLHttpRequest backend instead of the default fetch backend.

Angular’s built-in XSRF protection is enabled by default. For server-side rendering (SSR), retain the default fetch backend under current guidance. Angular warns that server-side XHR support is deprecated, is intended for removal in Angular 23, and carries redirect-security and denial-of-service concerns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Register interceptors deliberately

Interceptors let an application apply request or response handling across HTTP calls. Angular recommends functional interceptors because their ordering is more predictable, particularly in complex setups. Register them with withInterceptors([...]) inside provideHttpClient(...); requests pass through the list in the order specified. The interceptor guide covers the registration details.

provideHttpClient(
  withInterceptors([firstInterceptor, secondInterceptor]),
)

For an existing class-based interceptor, opt in explicitly with withInterceptorsFromDi() and provide the class using the HTTP_INTERCEPTORS multi-provider. Declaring an interceptor class alone does not add it to the HttpClient chain. DI-based interceptors run in provider registration order, which can be difficult to predict in extensive hierarchical injector configurations.

When a child injector should use the parent client

A child injector that provides its own HttpClient configuration normally uses that client for its requests rather than inheriting the parent chain. To run the child’s local interceptors and then send requests through the parent client, add withRequestsMadeViaParent() to the child configuration. This requires an HttpClient in the parent injector; otherwise Angular reports a runtime error.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Configure the test client separately

In a TestBed, use provideHttpClientTesting() from @angular/common/http/testing. It installs a test backend that captures requests so tests can assert on them and flush controlled success or error responses. HttpTestingController can also verify that no unexpected requests were made. See Angular’s HTTP testing guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a test needs client features such as interceptors, provide the configured client first and the testing provider second. The testing provider replaces parts of the client configuration, so the order matters:

TestBed.configureTestingModule({
  providers: [
    provideHttpClient(withInterceptors([authInterceptor])),
    provideHttpClientTesting(),
  ],
});

Keep this test-only provider in the testing setup rather than adding it to production application providers.

Common setup mistakes to avoid

  • Using an old setup recipe without checking the Angular version: the current guide says injection is provided by default from v21 onward, while older applications may need explicit provider configuration.
  • Adding an interceptor class but not enabling DI interceptors: use withInterceptorsFromDi() and register the class with HTTP_INTERCEPTORS, or migrate to a functional interceptor.
  • Reversing test provider order: place provideHttpClient(...) before provideHttpClientTesting().
  • Creating multiple clients across injectors without planning request flow: a child configuration can replace the parent client for child requests; use withRequestsMadeViaParent() when requests should traverse the parent chain.
  • Switching to XHR by habit: the default backend is fetch, and Angular’s current guidance warns against XHR for SSR.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.