Quick answer
To convert cURL to Axios, build a request config: -X sets method, each -H goes into headers, the query string becomes params, -d or --json becomes data, -u becomes auth and -m becomes timeout in milliseconds. Paste a cURL command into Playxoft's free cURL to Axios converter and it writes the axios() call with async/await or .then(), adding maxRedirects: 0 when the command has no -L, just like curl.
- Query to params
- maxRedirects like curl
- JSON, form and multipart data
- Basic auth and proxies
- Credential warnings
- async/await or .then()
How to use the cURL to Axios Converter
- 1Paste a command into cURL command, or choose one from Load example.
- 2Choose async/await or .then(), and leave Query as params on to get an editable params object.
- 3Keep Match curl redirects on to add maxRedirects: 0 for commands without -L, or turn it off to use Axios's default.
- 4Review Parsed request and the notes for anything Axios can't do in a browser, such as -k.
- 5Run
npm install axios, then copy the code or download request.js.
cURL flags cheat sheet: what does each option become in Axios?
| cURL | Axios config |
|---|---|
-X PATCH | method: "patch" |
-H "Name: value" | headers: { Name: "value" } |
?a=1&b=2 in the URL | params: { a: "1", b: "2" } |
--json '{…}' or a JSON -d | data: { … } (Axios serializes objects to JSON) |
-d "a=1&b=2" | data: new URLSearchParams({ a: "1", b: "2" }) |
-F name=@file | data: form, a FormData |
-u user:pass | auth: { username, password } |
-m 30 | timeout: 30000 |
No -L | maxRedirects: 0 |
-L --max-redirs 5 | maxRedirects: 5 |
-x http://host:8080 | proxy: { protocol, host, port } |
-k | httpsAgent: new https.Agent({ rejectUnauthorized: false }) |
Axios sets Content-Type: application/json for plain objects and application/x-www-form-urlencoded for URLSearchParams, so the converter doesn't repeat those headers. A Content-Type you set yourself with -H is always kept.
Why does the converted Axios code set maxRedirects: 0?
Because curl doesn't follow redirects unless you pass -L, and Axios does: in Node.js it follows up to 21 of them by default. A command without -L that hits a 301 or 302 shows you the redirect in curl but the final page in Axios, a quiet difference that wastes time when you're debugging an API.
With Match curl redirects on, commands without -L get maxRedirects: 0 so the code behaves exactly like curl. Two things to know:
- Axios then rejects the 3xx response, because its default
validateStatusonly accepts 2xx. Readerror.response.headers.locationto see where it pointed, or passvalidateStatus: (status) => status < 400. maxRedirectsonly applies in Node.js. In browsers, XMLHttpRequest always follows redirects and the option is ignored.
Axios vs fetch: when is Axios the better choice?
Axios earns its place when many requests share the same behavior:
- One client for everything.
axios.create({ baseURL, headers, timeout })gives you shared defaults, and interceptors can refresh tokens or log every call in one place. - Errors you can't ignore. Any non-2xx status rejects, so a failed call can't slip through as a success.
- Progress events.
onUploadProgressandonDownloadProgressdrive progress bars, in the browser and (since Axios 1.x) in Node.js.
The costs: a dependency in every bundle, and params serializes arrays as ids[]=1&ids[]=2 by default. That is why the converter keeps repeated query keys such as ?id=1&id=2 in the URL instead of moving them to params. For zero-dependency code, use the cURL to fetch converter.
How do I share one Axios instance with interceptors?
A converted command is a single call. In an app, the parts every call shares (base URL, auth header, timeout) belong in one instance:
import axios from "axios";
export const api = axios.create({
baseURL: "https://api.example.com/v1",
timeout: 30000,
});
api.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${getToken()}`;
return config;
});
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) signOut();
return Promise.reject(error);
},
);getToken() and signOut() stand for your own helpers. The converted call then shrinks to api.get("/orders", { params: { status: "shipped" } }), and the token never has to appear in source code, unlike the one in a copied command.
Copying requests from a browser? See how to copy as cURL in Chrome or Firefox DevTools, and turn on Drop browser headers to keep sec-ch-ua, sec-fetch-mode and priority out of the Axios config.
What should I check before sharing Axios code with tokens or -k?
The converter runs entirely in your browser and never sends or stores your command. The generated code is another matter:
- Credentials. The Contains credentials badge lists tokens, cookies and passwords found in the request, and their lines are marked in amber. Read them from the environment instead:
$API_TOKENin the command becomesprocess.env.API_TOKEN. - Basic auth.
auth: { username, password }is only base64-encoded on the wire, so use it over HTTPS. - Certificate checks. An
httpsAgentwithrejectUnauthorized: falseaccepts any certificate, including a forged one. Use it against a local test server, never in shipped code.
Frequently asked questions
Last updated by the Playxoft engineering team.