fetch or XMLHttpRequest APIs and provides a streamlined way to communicate with backend services.
Key Features
- Observable-based API: All HTTP methods return RxJS Observables for powerful async operations
- Request and response interceptors: Transform requests and responses globally
- Typed request and response objects: Full TypeScript support with generic types
- Streamlined error handling: Structured error responses
- Testability: Built-in testing utilities for mocking HTTP requests
- Progress events: Track upload and download progress
- Request cancellation: Cancel in-flight requests when needed
Getting Started
Import HttpClient
To use the HTTP client, you need to importHttpClient and configure it in your application:
Provide HTTP Client
In your application configuration, provide the HTTP client:Making Requests
TheHttpClient service provides methods for all common HTTP verbs:
get()- Retrieve data from a serverpost()- Send data to create a resourceput()- Send data to update a resourcepatch()- Send data to partially update a resourcedelete()- Delete a resourcehead()- Retrieve headers onlyoptions()- Retrieve supported HTTP methodsjsonp()- Make JSONP requests
Basic GET Request
POST Request with Body
Request Options
All HTTP methods accept an options object to customize the request:HttpHeaders | object
HTTP headers to send with the request
HttpParams | object
URL query parameters
'json' | 'text' | 'blob' | 'arraybuffer'
Expected response format (defaults to ‘json’)
'body' | 'response' | 'events'
What to observe in the response (defaults to ‘body’)
boolean
Whether to send credentials (cookies) with the request
boolean
Whether to report upload/download progress events
Response Types
By default,HttpClient assumes JSON responses, but you can specify other types:
JSON Response (default)
Text Response
Blob Response
Observing Responses
Control what part of the response you want to observe:Body Only (default)
Full Response
All Events
Error Handling
Handle HTTP errors using RxJS operators:HTTP Headers
Work with HTTP headers using theHttpHeaders class:
HttpHeaders is immutable, so set() returns a new instance.
URL Parameters
Add query parameters usingHttpParams or a plain object:
Request Cancellation
Cancel in-flight requests by unsubscribing:Related APIs
HttpClient
Complete API reference for the HttpClient service
HttpInterceptor
Intercept and transform HTTP requests and responses
