Why Axios code from curl
Axios is still the HTTP client of choice in many Node back ends and React or Vue front ends, mainly for its config object, interceptors and automatic JSON handling. When an API’s documentation, Postman or your browser’s network panel gives you a curl command, this converter writes the matching axios({ ... }) call, using Axios idioms instead of a literal transcription.
The config object, option by option
- method is written in lower case (
'post') and omitted for GET. - url and params: when every query parameter is plain (letters, digits,
-_.~) and unique, the query string is split into aparamsobject, so?page=2&limit=10becomesparams: { page: '2', limit: '10' }. Parameters that need encoding or repeat are left in the URL untouched, because re-encoding could change them. - headers keeps your
-Hvalues. Duplicate names are merged into one header. - data: a JSON body is turned into a JavaScript object literal, and curl’s implied JSON
Content-Typeis dropped because Axios adds it for objects. String bodies are kept verbatim; if a body declared as JSON is not trimmed, valid JSON,transformRequest: [(data) => data]stops Axios from re-serialising it. - auth:
-u user:passbecomesauth: { username, password }. - timeout comes from
-min milliseconds. - maxRedirects: 0 is set unless the command has
-L, matching curl’s default of not following redirects. - decompress: true reflects
--compressed. - proxy:
-x(and-Uor credentials in the proxy URL) become{ protocol, host, port, auth }. - httpsAgent:
-kimportsnode:httpsand passesnew https.Agent({ rejectUnauthorized: false }).
Multipart -F fields are appended to a FormData, with placeholders for files. The code ends by logging response.status and response.data.
Node-only settings
httpsAgent, proxy and maxRedirects only take effect when Axios runs in Node.js. In a browser, Axios uses XMLHttpRequest or fetch, which always follow redirects and always verify certificates. If the code is for a web page, remove those keys rather than relying on them. --connect-timeout has no Axios equivalent and is recorded as a comment.
Parsing details
The converter understands quoting from bash, zsh, Windows cmd and PowerShell, including line continuations and a leading $ prompt. Repeated -d values are joined with &; -G moves them into the query; --json adds both JSON headers. Data read from files (-d @body.json) cannot be loaded in the browser, so a clearly marked placeholder appears instead, with a warning. Unknown or unsupported options are listed rather than silently ignored.
Because commands usually carry live credentials, the whole conversion runs in your browser. For the built-in alternative without a dependency, see cURL to fetch.
Examples
Create an order
The query becomes params, the JSON string becomes an object literal in data, and the explicit Content-Type header is kept.
curl -X POST 'https://api.example.com/v1/orders?expand=items' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc' \
-d '{"customer":"cus_42","items":[{"sku":"KB-104","qty":1}]}'import axios from 'axios';
const response = await axios({
method: 'post',
url: 'https://api.example.com/v1/orders',
params: {
expand: 'items',
},
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc',
},
data: { customer: 'cus_42', items: [{ sku: 'KB-104', qty: 1 }] },
maxRedirects: 0, // curl follows redirects only with -L
});
console.log(response.status);
console.log(response.data);
Search behind a corporate proxy
Shows the proxy object with credentials, an https.Agent that skips certificate checks for -k, and a 15000 ms timeout.
curl --json '{"query":"keyboard","limit":10}' \
'https://search.example.com/api?page=2&lang=en' \
-x http://proxy.corp:3128 -U svc:hunter2 -k -m 15import axios from 'axios';
import https from 'node:https';
const response = await axios({
method: 'post',
url: 'https://search.example.com/api',
params: {
page: '2',
lang: 'en',
},
headers: {
Accept: 'application/json',
},
data: { query: 'keyboard', limit: 10 },
timeout: 15000,
maxRedirects: 0, // curl follows redirects only with -L
httpsAgent: new https.Agent({ rejectUnauthorized: false }), // curl -k: skip TLS certificate checks
proxy: {
protocol: 'http',
host: 'proxy.corp',
port: 3128,
auth: {
username: 'svc',
password: 'hunter2',
},
},
});
console.log(response.status);
console.log(response.data);
Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
curl cannot send -d data and -F form fields in the same request | The command mixes a url-encoded or raw body with multipart fields. | Use only -F for multipart uploads, or only -d for a plain body. |
Request data from the file "body.json" cannot be read here; the code contains a placeholder to replace | The command reads its body from a file with -d @file. | Replace the placeholder with the file contents, or read the file with fs.readFile in Node. |
The curl command has no URL | Only options were pasted, or the URL is inside an unquoted variable. | Add the URL, e.g. curl ‘https://api.example.com/items’. |
Expected a curl command, but it starts with "wget" | The input is not a curl command. | Paste a command that starts with curl (or just with a URL). |
Frequently asked questions
Why does the JSON body appear as an object instead of a string?
Axios serialises plain objects to JSON automatically, so an object literal is clearer and sends the same bytes. If the body was not clean JSON, it stays a string.
Will -k work in the browser?
No. The https.Agent only applies in Node.js; browsers always validate certificates.
Why is maxRedirects set to 0?
Your curl command does not include -L, so curl would stop at the first redirect. Remove the setting to let Axios follow redirects.
Does it keep repeated query parameters?
Yes. When names repeat or values need encoding, the query string stays in the URL exactly as written instead of being split into params.