HTTP client
Aurelia provides a powerful HTTP client through the aurelia-http-client package. This client offers a comfortable interface to the browser's XMLHttpRequest object, providing an easy way to make HTTP requests in your Aurelia applications.
Installation
Since the HTTP client is not included in Aurelia's default installation, you'll need to install it separately:
# Using npm
npm install aurelia-http-client
# Using yarn
yarn add aurelia-http-clientBasic Concepts
The HTTP client in Aurelia provides:
A fluent API for making HTTP requests
Support for all standard HTTP methods
Request/response interceptors
Configurable defaults
JSONP support
Progress tracking for uploads and downloads
Getting Started
Setup with Dependency Injection
The recommended way to use the HTTP client in Aurelia is through dependency injection. Here's how to set it up:
Alternative Setup
You can also create a new instance of the HTTP client directly, although this approach is less common in Aurelia applications:
Simple Request Examples
Here are some common examples of making HTTP requests:
Each request returns a Promise that resolves with an HttpResponseMessage object. The content property of this object contains the parsed response data.
Error Handling
It's important to implement proper error handling when making HTTP requests:
Making HTTP Requests
The HTTP client provides methods for all standard HTTP verbs and JSONP requests. Each method returns a Promise that resolves to an HttpResponseMessage.
Available HTTP Methods
JSONP Requests
JSONP requests are useful when you need to make cross-origin requests to services that don't support CORS. The HTTP client provides a dedicated jsonp method:
The second parameter ('callback') specifies the callback parameter name expected by the JSONP service.
Working with Request/Response Data
The HttpResponseMessage object provides several properties for handling response data:
Configuration
The HTTP client can be configured globally or per request. Configuration options include base URLs, headers, parameters, and more.
Global Configuration
Configure the HTTP client once for all requests:
Request-Specific Configuration
Configure individual requests using the createRequest method:
Available Configuration Options
The configuration API provides several chainable methods:
withBaseUrl(url)
Sets the base URL for requests
withHeader(key, value)
Adds a header to the request
withCredentials(value)
Enables/disables credentials
withTimeout(value)
Sets request timeout in milliseconds
withParams(params)
Adds query parameters
withResponseType(type)
Sets the expected response type
withReviver(reviver)
Sets a reviver function for JSON parsing
withReplacer(replacer)
Sets a replacer function for JSON stringifying
withProgressCallback(callback)
Sets a progress callback function
Request Building
The RequestBuilder provides a fluent API for creating and configuring individual HTTP requests with fine-grained control.
Using RequestBuilder
Available Request Builder Methods
HTTP Methods
asDelete(), asGet(), asHead(), asOptions(), asPatch(), asPost(), asPut(), asJsonp()
URL & Content
withUrl(), withBaseUrl(), withContent(), withParams()
Headers & Auth
withHeader(), withCredentials()
Response Handling
withResponseType(), withReviver(), withReplacer()
Callbacks & Timeouts
withProgressCallback(), withTimeout(), withCallbackParameterName()
Response Handling
The HTTP client provides comprehensive response handling capabilities through the HttpResponseMessage object.
Response Message Structure
Error Handling
Interceptors
Interceptors allow you to modify requests and responses globally, useful for adding authentication, logging, or error handling.
Creating and Configuring Interceptors
Authentication Interceptor Example
Interceptors are called in the order they are added. Each interceptor in the chain receives the result of the previous interceptor.
Multiple Interceptors
These interceptors can be used to implement cross-cutting concerns like:
Authentication
Logging
Error handling
Request/Response transformation
Performance monitoring
Cache handling
Advanced Usage
This section covers advanced scenarios and patterns for using the HTTP client effectively in your Aurelia applications.
Working with Different Content Types
Progress Tracking
Timeout Handling
Retry Logic with Interceptors
Caching Responses
Authentication with Refresh Token
Be careful with caching mechanisms as they might lead to stale data. Consider implementing cache invalidation strategies based on your application's needs.
These advanced patterns demonstrate handling complex scenarios while keeping your code organized and maintainable. The HTTP client's flexibility allows extensive customization to meet various application requirements.
Last updated
Was this helpful?