What Is Axios? β Definition and How It Works
Learn what Axios is, how interceptors and automatic JSON handling work, how it differs from fetch, and what it does not do for scraping.
Axios is a promise-based HTTP client for JavaScript, running in both browsers and Node.js. It predates fetch being universally available, and it survives because of the ergonomics it adds rather than the requests it makes.
1. What Is Axios?
A wrapper over the platform's HTTP mechanism β XMLHttpRequest in browsers, the http module in Node β with a consistent API across both.
const { data } = await axios.get('https://api.example.com/products', {
params: { page: 1 },
headers: { Authorization: `Bearer ${token}` },
timeout: 5000
});
Three things happened automatically there: the query string was built from an object, the JSON response was parsed, and a timeout was enforced. All three require explicit handling with fetch.
2. Axios vs. fetch
fetch |
Axios | |
|---|---|---|
| Built in | Yes | Dependency |
| JSON parsing | Manual .json() |
Automatic |
| Rejects on 4xx/5xx | No | Yes |
| Timeout | Via AbortSignal |
timeout option |
| Query params | Build the string | params object |
| Interceptors | No | Yes |
| Upload progress | No | Yes |
| Node and browser | Yes (modern) | Yes |
The row that causes the most real bugs is the third. fetch resolves successfully on a 404 or a 500 β response.ok must be checked explicitly, and code that forgets treats an error page as data. Axios rejects, so a try/catch catches HTTP errors as well as network ones.
3. Interceptors
Axios's most distinctive feature: functions that run on every request or response.
axios.interceptors.request.use(config => {
config.headers.Authorization = `Bearer ${getToken()}`;
return config;
});
axios.interceptors.response.use(
r => r,
async error => {
if (error.response?.status === 401) {
await refreshToken();
return axios(error.config); // retry the original request
}
return Promise.reject(error);
}
);
Authentication, token refresh, logging, retry and error normalisation live in one place instead of at every call site. This is the main reason Axios persists in codebases that could otherwise use fetch.
4. Instances
Configuration can be bound to a client:
const api = axios.create({
baseURL: 'https://api.example.com/v2',
timeout: 10000,
headers: { 'X-Client': 'my-app' }
});
Each service gets its own instance with its own base URL, credentials and interceptors β cleaner than threading options through every call, and it keeps one API's auth logic from leaking into another's.
5. Errors
The error object distinguishes failure modes:
try {
await api.get('/products');
} catch (err) {
if (err.response) // server responded with 4xx/5xx
console.error(err.response.status, err.response.data);
else if (err.request) // request sent, no response β timeout, DNS, refused
console.error('no response');
else // misconfiguration before sending
console.error(err.message);
}
The distinction matters for retry logic: err.response with a 500 is worth retrying, err.response with a 400 is not, and err.request usually is.
6. What It Does Not Do for Scraping
Worth being explicit, because Axios is often reached for and then found wanting.
No JavaScript execution. It retrieves HTML. Client-rendered content is not there.
No HTML parsing. Pair it with Cheerio.
No TLS impersonation. In Node, Axios uses Node's TLS stack, which has a fingerprint identifying it as Node β before any header is sent. A perfect Chrome User-Agent on an Axios request is a contradiction, and a strong bot signal. No Axios configuration changes this; it requires a different client library entirely.
No retries. axios-retry adds them, or an interceptor does.
No rate limiting. Yours to implement.
For scraping specifically, Axios plus Cheerio is a fine stack against undefended targets and the wrong stack against fingerprinting ones.
7. Real-World Examples
- A token refresh implemented once in a response interceptor, serving an entire application.
- A
fetchmigration that introduced silent failures, because 404s stopped throwing and error pages were parsed as data. - Per-service instances keeping three APIs' credentials and base URLs cleanly separated.
- An Axios scraper blocked despite perfect headers, identified by Node's TLS fingerprint.
- Retry logic centralised in an interceptor, with backoff applied to 5xx and 429 and not to 4xx.
8. Summary
Axios is a promise-based HTTP client for browsers and Node with automatic JSON handling, real timeouts, query parameter serialisation, and rejection on HTTP error status β the last being a meaningful safety improvement over fetch, which resolves on 404s and 500s.
Interceptors and instances are its lasting advantage: cross-cutting concerns like authentication, refresh, logging and retry live in one place rather than at every call site.
For data collection it fetches and nothing more. It does not render, does not parse, and does not disguise Node's TLS fingerprint β which is why it works well against open targets and not at all against ones that fingerprint their clients.