axios — Promise-Based HTTP Client for Browser & Node.js
axios is the most popular HTTP client for JavaScript — promise-based, isomorphic (browser + Node), with request/response interceptors, automatic JSON transforms, CSRF protection, and built-in cancellation. Battle-tested since 2014.
Staging sûr pour cet actif
Cet actif est d'abord staged. Le prompt copié demande à l'agent d'inspecter les fichiers staged avant d'activer scripts, config MCP ou config globale.
npx -y tokrepo@latest install b3e550af-3588-11f1-9bc6-00163e2b0d79 --target codexStage les fichiers d'abord; l'activation exige la revue du README et du plan staged.
What it is
axios is a promise-based HTTP client for JavaScript that works in both browsers and Node.js. It provides request and response interceptors, automatic JSON transformation, CSRF protection, request cancellation, and timeout handling. Battle-tested since 2014, axios is the most widely used HTTP client in the JavaScript ecosystem.
Frontend and backend JavaScript developers who need a consistent HTTP interface across environments benefit most. Whether you are calling REST APIs from a React app or making server-to-server requests in Node.js, axios provides the same API surface.
How it saves time or tokens
axios eliminates the boilerplate of native fetch calls: no manual JSON.stringify for request bodies, no response.json() calls, no separate error handling for network errors vs HTTP errors. Interceptors let you inject auth tokens, logging, or retry logic in one place rather than at every call site. The isomorphic design means you write one HTTP layer that works in SSR and client-side rendering without conditional imports.
How to use
- Install axios:
npm install axios
- Make a GET request:
import axios from 'axios';
const response = await axios.get('https://api.example.com/users');
console.log(response.data); // Already parsed JSON
- Configure a reusable instance with interceptors:
const api = axios.create({
baseURL: 'https://api.example.com',
timeout: 5000
});
api.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${getToken()}`;
return config;
});
Example
// POST with automatic JSON serialization
await axios.post('/api/users', {
name: 'Jane',
email: 'jane@example.com'
});
// Concurrent requests
const [users, posts] = await Promise.all([
axios.get('/api/users'),
axios.get('/api/posts')
]);
// Error handling with response details
try {
await axios.get('/api/protected');
} catch (error) {
if (error.response) {
console.log(error.response.status); // 401
console.log(error.response.data); // Error body
}
}
// Cancel a request
const controller = new AbortController();
axios.get('/api/slow', { signal: controller.signal });
controller.abort();
Related on TokRepo
- AI Coding Tools -- Developer libraries and utilities on TokRepo
- API Integration Tools -- API clients and integration frameworks
Common pitfalls
- axios does not throw on HTTP 4xx/5xx status codes by default in older versions. In current versions (1.x), it does throw. Check your version's behavior and configure
validateStatusif needed. - The default timeout is 0 (no timeout). Always set an explicit timeout in production to avoid hanging requests.
- axios instances share interceptors added after creation. If you modify interceptors dynamically, each API call may behave differently depending on timing.
Questions fréquentes
axios provides automatic JSON parsing, request/response interceptors, timeout support, and consistent error handling out of the box. fetch requires manual JSON handling, has no interceptor pattern, and does not reject on HTTP errors. axios works identically in Node.js and browsers; fetch needs a polyfill in older Node versions.
Yes. axios ships with built-in TypeScript type definitions. You can type request and response data using generics: axios.get<User[]>('/api/users') returns a typed response. The interceptor types are also fully typed.
Yes. axios is framework-agnostic. Import it in any React, Vue, Angular, or Svelte component. For server-side rendering frameworks like Next.js or Nuxt, axios works on both server and client sides with the same API.
Use the axios-retry plugin or implement retry logic in a response interceptor. axios-retry adds automatic retries with exponential backoff for network errors and 5xx responses. Install it alongside axios and configure the retry count and delay.
Yes. axios continues to receive updates and security patches. The 1.x line is stable and widely used. The library has a large contributor base and remains the most downloaded HTTP client on npm.
Sources citées (3)
- axios GitHub Repository— axios is the most popular HTTP client for JavaScript
- axios Official Documentation— axios provides interceptors, automatic JSON, and CSRF protection
- axios API Documentation— axios is isomorphic for browser and Node.js environments
En lien sur TokRepo
Fil de discussion
Actifs similaires
Got — Human-Friendly HTTP Client for Node.js
Got is a lightweight, feature-rich HTTP client for Node.js with built-in retry logic, pagination, caching, and hooks for composable request pipelines.
OkHttp — Modern HTTP Client for Java and Kotlin
A reliable and efficient HTTP client for the JVM and Android with connection pooling, transparent GZIP, response caching, and WebSocket support.
JSZip — Create and Read ZIP Files in JavaScript
A JavaScript library for creating, reading, and editing ZIP archives in the browser and Node.js with an intuitive promise-based API.
Execa — Process Execution for Humans in Node.js
A modern process execution library for Node.js that improves on child_process with better defaults, a Promise-based API, and ergonomic pipe and stream handling.