Create HttpClient instances with baseURL and default headers
đ Code Examples:
// Create an instance with baseURL and headers
const api = HttpClient.create({
baseURL: 'https://jsonplaceholder.typicode.com',
headers: {
'Authorization': 'Bearer your-token-here',
'X-API-Version': '1.0',
'Content-Type': 'application/json'
}
});
// Use the instance (baseURL is automatically prepended)
const response = await api.get('/posts/2'); // GET https://jsonplaceholder.typicode.com/posts/2
// POST with instance defaults
const newPost = { title: 'Instance Post', body: 'Content', userId: 1 };
const postResponse = await api.post('/posts', newPost);
đ 3. Global Headers
Set headers that apply to all requests
đ Code Examples:
// Set global headers that apply to all requests
HttpClient.setHeader('X-Global-Header', 'global-value');
HttpClient.setHeader('User-Agent', 'advanced-http-client/1.0.0');
// These headers will be sent with every request
const response = await HttpClient.get('https://httpbin.org/headers');
console.log('Headers sent:', response.data.headers);
// Global headers can be overridden by per-request headers
const response2 = await HttpClient.get('https://httpbin.org/headers', {
headers: { 'X-Global-Header': 'override-value' }
});
đ 4. Header Precedence
Test header priority: request > instance > global
đ Code Examples:
// Header precedence: request > instance > global
// Set global header
HttpClient.setHeader('X-Header', 'global-value');
// Create instance with different header value
const api = HttpClient.create({
headers: {
'X-Header': 'instance-value',
'X-Instance-Only': 'instance-only'
}
});
// Request headers override instance and global
const response = await api.get('https://httpbin.org/headers', {
headers: {
'X-Header': 'request-value', // Overrides instance and global
'X-Request-Only': 'request-only' // New header
}
});
// Result: X-Header = 'request-value' (request wins)
// Result: X-Instance-Only = 'instance-only' (from instance)
// Result: X-Request-Only = 'request-only' (from request)
đ 5. Isolated Requests
Requests that ignore global and instance settings
đ Code Examples:
// Completely isolated request - ignores all global/instance settings
const response = await HttpClient.post(
'https://httpbin.org/post',
{ message: 'isolated request' },
{
isolated: true,
headers: { 'X-Isolated': 'isolated-only' }
}
);
// Isolated request with selective header inclusion
const api = HttpClient.create({
baseURL: 'https://jsonplaceholder.typicode.com',
headers: { 'Authorization': 'Bearer token' }
});
const selectiveResponse = await api.post(
'https://jsonplaceholder.typicode.com/posts', // Use full URL in isolated mode
{ title: 'Selective Headers Post' },
{
isolated: true,
includeHeaders: ['Authorization'], // Only include this header from global/instance
headers: { 'X-Selective': 'selective-only' }
}
);
Start requests with controlKey and cancel them programmatically
đ Code Example:
// Start a cancellable request (with controlKey)
const ctrlKey = HttpClient.generateControlKey();
const withKey = HttpClient.get('https://httpbin.org/delay/5', { controlKey: ctrlKey });
// Later, cancel that specific one
HttpClient.cancelRequest(ctrlKey);
// Start a request WITHOUT a controlKey
const noKey = HttpClient.get('https://httpbin.org/delay/5');
// Cancel every pending request (with or without keys)
HttpClient.cancelAllRequests();