Response types
Response Type Detection
import { IHttpClient } from '@aurelia/fetch-client';
import { resolve } from '@aurelia/kernel';
export class ApiService {
private http = resolve(IHttpClient);
async fetchWithTypeDetection(url: string) {
const response = await this.http.get(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
return await response.json();
} else if (contentType.includes('text/')) {
return await response.text();
} else if (contentType.includes('image/') || contentType.includes('application/octet-stream')) {
return await response.blob();
} else {
return await response.arrayBuffer();
}
}
}JSON Responses
Binary Data (Images, Files, Documents)
Text Responses
ArrayBuffer for Raw Binary Data
Streaming Responses
Response Transformation with Interceptors
Error Response Handling
Best Practices
Content-Type Validation
Memory Management
Type Safety
Error Boundaries
Last updated
Was this helpful?