- Spec files
npm-axios
Describes: pkg:npm/axios@0.30.x
- Description
- Promise based HTTP client for the browser and node.js
- Author
- tessl
- Last updated
http-client.md docs/
1# HTTP Client and Requests23Core HTTP client functionality providing a comprehensive, Promise-based interface for making HTTP requests in both browser and Node.js environments.45## Capabilities67### Main Axios Instance89The default axios instance that can be called as a function or used as an object with method properties.1011```javascript { .api }12/**13* Make HTTP request using configuration object14* @param config - Complete request configuration15* @returns Promise resolving to response object16*/17axios(config: AxiosRequestConfig): Promise<AxiosResponse>;1819/**20* Make HTTP request with URL and optional configuration21* @param url - Request URL22* @param config - Optional request configuration23* @returns Promise resolving to response object24*/25axios(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;26```2728**Usage Examples:**2930```javascript31import axios from 'axios';3233// Request with config object34const response = await axios({35method: 'get',36url: 'https://api.example.com/users',37timeout: 5000,38headers: {39'Authorization': 'Bearer token123'40}41});4243// Request with URL and config44const response = await axios('https://api.example.com/users', {45method: 'post',46data: { name: 'John', email: 'john@example.com' }47});48```4950### HTTP Method Aliases - GET-like Methods5152HTTP methods that typically don't send request body data.5354```javascript { .api }55/**56* Make GET request57* @param url - Request URL58* @param config - Optional request configuration59* @returns Promise resolving to response object60*/61axios.get(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;6263/**64* Make DELETE request65* @param url - Request URL66* @param config - Optional request configuration67* @returns Promise resolving to response object68*/69axios.delete(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;7071/**72* Make HEAD request73* @param url - Request URL74* @param config - Optional request configuration75* @returns Promise resolving to response object76*/77axios.head(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;7879/**80* Make OPTIONS request81* @param url - Request URL82* @param config - Optional request configuration83* @returns Promise resolving to response object84*/85axios.options(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;86```8788**Usage Examples:**8990```javascript91// GET request with query parameters92const users = await axios.get('https://api.example.com/users', {93params: { page: 1, limit: 10 }94});9596// DELETE request with authorization97const result = await axios.delete('https://api.example.com/users/123', {98headers: { 'Authorization': 'Bearer token123' }99});100101// HEAD request to check if resource exists102const headResponse = await axios.head('https://api.example.com/users/123');103console.log(headResponse.status); // 200 if exists, 404 if not104105// OPTIONS request to check allowed methods106const optionsResponse = await axios.options('https://api.example.com/users');107console.log(optionsResponse.headers['allow']);108```109110### HTTP Method Aliases - POST-like Methods111112HTTP methods that send data in the request body.113114```javascript { .api }115/**116* Make POST request117* @param url - Request URL118* @param data - Data to send in request body119* @param config - Optional request configuration120* @returns Promise resolving to response object121*/122axios.post(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;123124/**125* Make PUT request126* @param url - Request URL127* @param data - Data to send in request body128* @param config - Optional request configuration129* @returns Promise resolving to response object130*/131axios.put(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;132133/**134* Make PATCH request135* @param url - Request URL136* @param data - Data to send in request body137* @param config - Optional request configuration138* @returns Promise resolving to response object139*/140axios.patch(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;141```142143**Usage Examples:**144145```javascript146// POST request with JSON data147const newUser = await axios.post('https://api.example.com/users', {148name: 'Alice Johnson',149email: 'alice@example.com',150role: 'admin'151});152153// PUT request to update entire resource154const updatedUser = await axios.put('https://api.example.com/users/123', {155name: 'Alice Smith',156email: 'alice.smith@example.com',157role: 'moderator'158});159160// PATCH request to partially update resource161const patchedUser = await axios.patch('https://api.example.com/users/123', {162role: 'admin'163});164```165166### Form Data Methods167168Convenience methods for sending form data with automatic `multipart/form-data` content type.169170```javascript { .api }171/**172* Make POST request with form data173* @param url - Request URL174* @param data - Form data to send175* @param config - Optional request configuration176* @returns Promise resolving to response object177*/178axios.postForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;179180/**181* Make PUT request with form data182* @param url - Request URL183* @param data - Form data to send184* @param config - Optional request configuration185* @returns Promise resolving to response object186*/187axios.putForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;188189/**190* Make PATCH request with form data191* @param url - Request URL192* @param data - Form data to send193* @param config - Optional request configuration194* @returns Promise resolving to response object195*/196axios.patchForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;197```198199**Usage Examples:**200201```javascript202// Post form data with file upload203const formData = new FormData();204formData.append('name', 'John Doe');205formData.append('avatar', fileInput.files[0]);206207const response = await axios.postForm('https://api.example.com/users', formData);208209// Post object data as form (automatically converted)210const response = await axios.postForm('https://api.example.com/users', {211name: 'Jane Doe',212email: 'jane@example.com'213});214```215216### Core Request Method217218The underlying request method used by all HTTP method aliases.219220```javascript { .api }221/**222* Core request method with full configuration support223* @param config - Complete request configuration224* @returns Promise resolving to response object225*/226axios.request(config: AxiosRequestConfig): Promise<AxiosResponse>;227```228229**Usage Examples:**230231```javascript232// Custom request with full configuration233const response = await axios.request({234method: 'get',235url: 'https://api.example.com/data',236timeout: 10000,237headers: {238'Accept': 'application/json',239'User-Agent': 'MyApp/1.0'240},241params: { format: 'json' },242validateStatus: (status) => status < 500243});244```245246### URI Builder247248Build complete URI from configuration without making a request.249250```javascript { .api }251/**252* Build URI from configuration object253* @param config - Request configuration with URL components254* @returns Complete URI string255*/256axios.getUri(config?: AxiosRequestConfig): string;257```258259**Usage Examples:**260261```javascript262// Build URI with query parameters263const uri = axios.getUri({264url: '/users',265baseURL: 'https://api.example.com',266params: { page: 2, limit: 50 }267});268console.log(uri); // https://api.example.com/users?page=2&limit=50269270// Build URI for complex request271const uri = axios.getUri({272method: 'post',273baseURL: 'https://api.example.com',274url: '/users',275params: { include: 'profile' }276});277```278279### Instance Creation280281Create new axios instances with custom default configuration.282283```javascript { .api }284/**285* Create new axios instance with custom defaults286* @param config - Default configuration for the instance287* @returns New axios instance288*/289axios.create(config?: AxiosRequestConfig): AxiosInstance;290```291292**Usage Examples:**293294```javascript295// Create instance with base URL and default headers296const apiClient = axios.create({297baseURL: 'https://api.example.com',298timeout: 5000,299headers: {300'Authorization': 'Bearer your-token',301'Content-Type': 'application/json'302}303});304305// Use instance like main axios306const users = await apiClient.get('/users');307const newUser = await apiClient.post('/users', userData);308309// Create instance for different API310const authClient = axios.create({311baseURL: 'https://auth.example.com',312timeout: 3000313});314```315316## Response Object Structure317318All HTTP methods return a Promise that resolves to an AxiosResponse object:319320```javascript { .api }321interface AxiosResponse<T = any, D = any> {322/** Response data (parsed JSON or raw data based on responseType) */323data: T;324/** HTTP status code */325status: number;326/** HTTP status message */327statusText: string;328/** Response headers */329headers: AxiosResponseHeaders;330/** Request configuration that generated this response */331config: AxiosRequestConfig<D>;332/** Platform-specific request object (XMLHttpRequest or Node.js request) */333request?: any;334}335```336337**Usage Examples:**338339```javascript340const response = await axios.get('https://api.example.com/users/123');341342console.log(response.data); // User object or data343console.log(response.status); // 200, 404, etc.344console.log(response.statusText); // 'OK', 'Not Found', etc.345console.log(response.headers); // Response headers object346console.log(response.config); // Request configuration used347```348349## TypeScript Generic Support350351Axios supports TypeScript generics for type-safe request and response handling:352353```typescript { .api }354interface AxiosInstance {355<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;356<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;357358get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;359post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;360put<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;361patch<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;362delete<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;363head<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;364options<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;365postForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;366putForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;367patchForm<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;368}369```370371**TypeScript Usage Examples:**372373```typescript374import axios, { AxiosResponse } from 'axios';375376interface User {377id: number;378name: string;379email: string;380}381382interface CreateUserData {383name: string;384email: string;385}386387// Type-safe GET request388const response: AxiosResponse<User> = await axios.get<User>('/users/123');389const user: User = response.data; // Type-safe access390391// Type-safe POST request392const newUser = await axios.post<User, AxiosResponse<User>, CreateUserData>(393'/users',394{ name: 'John', email: 'john@example.com' }395);396```