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-client
Basic 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
By default, the HTTP client assumes you are expecting JSON responses and will automatically parse JSON response bodies.
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:
You can also create a new instance of the HTTP client directly, although this approach is less common in Aurelia applications:
import { HttpClient } from 'aurelia-http-client';
export class MyClass {
constructor() {
this.http = new HttpClient();
}
}
Simple Request Examples
Here are some common examples of making HTTP requests:
// GET request
async function getUser(id) {
const response = await this.http.get(`api/users/${id}`);
return response.content;
}
// POST request with data
async function createUser(userData) {
const response = await this.http.post('api/users', userData);
return response.content;
}
// PUT request
async function updateUser(id, userData) {
const response = await this.http.put(`api/users/${id}`, userData);
return response.content;
}
// DELETE request
async function deleteUser(id) {
const response = await this.http.delete(`api/users/${id}`);
return response.content;
}
Each request returns a Promise that resolves with an HttpResponseMessage object. The content property of this object contains the parsed response data.
You don't need to parse response data manually when working with JSON APIs. The HTTP client automatically handles JSON parsing for you when the response contains the appropriate content-type header.
Error Handling
It's important to implement proper error handling when 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
@inject(HttpClient)
export class ApiService {
constructor(http) {
this.http = http;
}
// GET request
get(url) {
return this.http.get(url);
}
// POST request with data
post(url, data) {
return this.http.post(url, data);
}
// PUT request with data
put(url, data) {
return this.http.put(url, data);
}
// PATCH request with data
patch(url, data) {
return this.http.patch(url, data);
}
// DELETE request
delete(url) {
return this.http.delete(url);
}
// HEAD request
head(url) {
return this.http.head(url);
}
// OPTIONS request
options(url) {
return this.http.options(url);
}
}
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:
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.