The HttpClient Service provides Angular's primary API for making HTTP requests. It offers a simplified, RxJS-based interface for communicating with backend servers, supporting both traditional XHR and modern Fetch API backends. This document covers the HttpClient class, its backend implementations, request/response handling, and the reactive httpResource API.
For information about HTTP security features (XSRF, XSSI), see HTTP Security Features. For the Resource API patterns used by httpResource, see Resource and httpResource APIs.
The HttpClient system follows a layered architecture where the HttpClient service delegates to backend implementations through the HttpHandler interface.
Architecture Diagram: HttpClient component hierarchy
Sources: packages/common/http/src/client.ts121-122 packages/common/http/src/fetch.ts74-83 packages/common/http/src/xhr.ts108-113 packages/common/http/src/backend.ts13-17 packages/common/http/src/provider.ts117-132
The HttpClient creates HttpRequest objects and passes them through a chain of interceptors before reaching a backend implementation. The backend translates the request into native browser APIs (Fetch or XHR) and returns an Observable<HttpEvent>.
The HttpClient class in packages/common/http/src/client.ts121-1200 provides HTTP methods as instance methods that return RxJS Observables.
| Method | Purpose | Request Body |
|---|---|---|
get() | Retrieve data | None |
post() | Create resource | Required |
put() | Update resource | Required |
patch() | Partial update | Required |
delete() | Remove resource | Optional |
head() | Headers only | None |
options() | Allowed methods | None |
request() | Generic request | Optional |
Sources: packages/common/http/src/client.ts124-1200
Diagram: HttpClient method overload resolution
Each method has multiple overloads that vary based on:
observe: Controls return type ('body', 'response', or 'events') packages/common/http/src/client.ts27responseType: Determines how to parse the response body packages/common/http/src/request.ts100Sources: packages/common/http/src/client.ts142-250 packages/common/http/src/request.ts109-141
Sources: packages/common/http/src/request.ts109-141 packages/common/http/src/client.ts25-31
Angular provides two backend implementations that translate HttpRequest objects into native browser APIs.
| Feature | FetchBackend | HttpXhrBackend |
|---|---|---|
| Browser API | fetch() | XMLHttpRequest |
| File location | packages/common/http/src/fetch.ts74 | packages/common/http/src/xhr.ts108 |
| Upload progress | ❌ Not supported fetch.ts39 | ✅ Supported xhr.ts35-36 |
| Modern options | ✅ Full support | ⚠️ Limited support xhr.ts44-98 |
| Node.js support | ✅ v18+ fetch.ts67 | ❌ Browser only |
| SSR recommended | ✅ Yes provider.ts78-81 | ❌ No |
Diagram: FetchBackend request processing flow
The FetchBackend in packages/common/http/src/fetch.ts74-400:
AbortController for cancellation packages/common/http/src/fetch.ts86fetchImpl (defaults to globalThis.fetch) packages/common/http/src/fetch.ts125-127ReadableStream packages/common/http/src/fetch.ts163reportDownloadProgress: true packages/common/http/src/fetch.ts159-161responseType packages/common/http/src/fetch.ts320-352Key implementation details:
destroyRef.destroyed to prevent unnecessary work packages/common/http/src/fetch.ts198-200Sources: packages/common/http/src/fetch.ts74-400
Diagram: HttpXhrBackend request processing
The HttpXhrBackend in packages/common/http/src/xhr.ts108-444:
Accept and Content-Type packages/common/http/src/xhr.ts165-179Notable features:
HttpHeaderResponse to avoid duplicate parsing packages/common/http/src/xhr.ts206-227Sources: packages/common/http/src/xhr.ts108-444
The HttpRequest<T> class in packages/common/http/src/request.ts153-700 represents an immutable HTTP request.
Key Properties:
| Property | Type | Description |
|---|---|---|
method | string | HTTP method (GET, POST, etc.) |
url | string | Request URL without query params |
urlWithParams | string | Full URL with query string |
headers | HttpHeaders | Request headers |
body | T | null | Request body |
params | HttpParams | URL query parameters |
responseType | HttpResponseType | Expected response format |
reportDownloadProgress | boolean | Enable download progress events |
withCredentials | boolean | Send cookies/auth |
context | HttpContext | Request metadata |
Sources: packages/common/http/src/request.ts153-400
The HttpHeaders class in packages/common/http/src/headers.ts provides immutable header management.
Implementation Details:
\n-separated header strings.Headers API.Sources: packages/common/http/src/headers.ts24-290
The HttpParams class in packages/common/http/src/params.ts159-420 handles URL query parameters.
Sources: packages/common/http/src/params.ts159-420
Interceptors allow request/response transformation and side effects.
Diagram: HTTP interceptor chain execution
Sources: packages/common/http/src/interceptor.ts126-146 packages/common/http/src/backend.ts20-50
Sources: packages/common/http/src/interceptor.ts126-129
Interceptors are registered via the provideHttpClient function using withInterceptors:
Sources: packages/common/http/src/provider.ts149-162
| Function | Purpose |
|---|---|
provideHttpClient() | Configure HttpClient with features provider.ts99 |
withXhr() | Use HttpXhrBackend provider.ts316 |
withInterceptors() | Register functional interceptors provider.ts149 |
withInterceptorsFromDi() | Use DI-based interceptors provider.ts179 |
withJsonpSupport() | Enable JSONP requests provider.ts249 |
withNoXsrfProtection() | Disable XSRF protection provider.ts230 |
withXsrfConfiguration() | Configure XSRF names provider.ts205 |
Sources: packages/common/http/src/provider.ts99-325
Both backends run network operations outside Angular's zone to minimize change detection cycles:
fetch() in NgZone.runOutsideAngular() packages/common/http/src/fetch.ts125-127TracingService to propagate context without triggering unnecessary cycles packages/common/http/src/xhr.ts109-117Sources: packages/common/http/src/fetch.ts125-127 packages/common/http/src/xhr.ts109-117
Refresh this wiki