Sub topic 1
Starting point: HttpClient and provideHttpClient
Interceptors work on top of HttpClient, so this chapter sets up the base they plug into. You will configure HttpClient, make a request, and see where interceptors will later attach.
Prerequisites
This chapter assumes you already know:
- TypeScript fundamentals.
- Angular components, services, and dependency injection.
- How to make GET and POST requests with
HttpClient. - Basic RxJS: Observables,
pipe, and operators such astapandcatchError.
The examples use standalone Angular applications, which bootstrap with bootstrapApplication instead of an NgModule.
What HttpClient is
HttpClient is Angular's service for sending HTTP requests. Each method, such as get, post, or put, returns an Observable. The Observable is cold: nothing is sent until something subscribes to it, and each subscription sends its own request.
Components rarely call HttpClient directly. A service usually wraps it, and components call the service.
Providing HttpClient
HttpClient is not available automatically. You register it with provideHttpClient() in the application's providers:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});provideHttpClient() accepts optional feature functions as arguments. Each one turns on an extra capability. The one this tutorial uses most is withInterceptors, which registers interceptors. You will use it in a later chapter.
In older, module-based applications you would import HttpClientModule instead. That approach is deprecated, so new code should use provideHttpClient.
Making a request
Inject HttpClient with inject() and return the Observable from the service:
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
export interface Todo {
id: number;
title: string;
done: boolean;
}
@Injectable({ providedIn: 'root' })
export class TodoService {
private readonly http = inject(HttpClient);
getTodos(): Observable<Todo[]> {
return this.http.get<Todo[]>('/api/todos');
}
}A component then subscribes, or uses the async pipe, and the request is sent at that point.
By default HttpClient parses the response body as JSON and emits it. The type argument <Todo[]> only tells TypeScript what to expect. It does not validate the data at runtime.
Why this matters for interceptors
Every request made through HttpClient passes through one pipeline before it reaches the network. An interceptor is a function you insert into that pipeline. It can read or change each request on the way out and each response on the way back. You write that logic once and every call picks it up. You do not repeat it in each service.
This also sets the limit. Only requests made through HttpClient are intercepted. Code that calls fetch or XMLHttpRequest directly bypasses the pipeline.
Common errors at this stage
NullInjectorError: No provider for HttpClient:provideHttpClient()is missing from the providers. Add it tobootstrapApplication, or to the app config if you use one.- Nothing appears in the network tab: the Observable was never subscribed to. Subscribe, or use the
asyncpipe. - Duplicate requests: two subscriptions to the same cold Observable send two requests. This is expected behavior. Share the result if you want one request.
- Configuration seems ignored: call
provideHttpClientonce, in the root providers. Providing it again in a lower injector creates a separateHttpClientwith its own configuration.
Exercise
A component calls this.http.get('/api/todos') and the browser's network tab shows no request. The app throws no errors. What is the most likely cause, and how do you fix it?
Show answer
The returned Observable is cold and has not been subscribed to. Subscribe to it, or bind it in the template with the async pipe, and the request will be sent. If instead the app threw NullInjectorError: No provider for HttpClient, the cause would be a missing provideHttpClient().
Exercise
Why does provideHttpClient() matter for interceptors, and what kind of request will an interceptor never see?
Show answer
Interceptors are registered through provideHttpClient using the withInterceptors feature, so HttpClient must be provided first. An interceptor never sees requests that bypass HttpClient, such as direct fetch or XMLHttpRequest calls.