Convert cURL to Axios

Paste a curl command and get the equivalent axios call — axios.get(), axios.post() and friends with a config object — in async/await or promise style.

Loading tool…

About the cURL to Axios

The command is read the way a shell reads it: single and double quotes, backslash escapes, ANSI-C strings ($'…', used by Chrome and Firefox in “Copy as cURL” for bodies with line breaks or quotes), line continuations, and the ^ escapes of “Copy as cURL (cmd)” on Windows. The options are then interpreted like curl does: -d makes the request a POST with the content type application/x-www-form-urlencoded, several -d are joined with &, --data-urlencode percent-encodes its value, -G moves the data into the query string, --json sets the Content-Type and Accept headers, -F builds a multipart form, -u becomes HTTP Basic authentication and -I a HEAD request.

The options map to the axios request config: headers to headers, -u user:password to auth, --max-time to timeout in milliseconds, -x to proxy, --max-redirs to maxRedirects, and -k to an https.Agent with rejectUnauthorized: false for Node.js. A JSON body is passed as a plain object — axios serializes it — when that produces the same data; a form body becomes URLSearchParams, and -F becomes FormData. When a body must be sent byte for byte, for example JSON with numbers beyond 253, it stays a string and a transformRequest keeps axios from re-encoding it.

Two differences from curl are worth knowing. axios rejects the promise for any status outside 200–299, while curl prints the error response and exits with 0 unless --fail is given; set validateStatus to change that. And axios follows up to 21 redirects in Node.js (the browser decides on its own), while curl follows none without -L. Options without an equivalent — client certificates, --resolve, retries — are listed under the code instead of being dropped silently. The same converter also writes fetch, XMLHttpRequest and Python requests code; nothing is uploaded.

How to use it

  1. Paste a curl command, for example from the developer tools: Network → Copy → Copy as cURL.
  2. Keep axios as the target, or switch to fetch, XMLHttpRequest or Python requests.
  3. Choose async/await or promise .then() style.
  4. Check the warnings and notes under the code, then copy it into your project (npm install axios).

Frequently asked questions

How do I send a curl -d JSON body with axios?
Pass the object as the second argument: axios.post(url, { name: "Ada" }). axios serializes it and sets Content-Type: application/json. The converter does this when the command has a JSON content type and the body is valid JSON.
How is curl -u converted?
To the auth option: auth: { username: "user", password: "secret" }. axios then sends the Authorization: Basic header. A bearer token given with --oauth2-bearer or -H becomes an Authorization header.
How do I send application/x-www-form-urlencoded data with axios?
Pass a URLSearchParams object, or a string, as the data. A plain object would be sent as JSON. The converter writes new URLSearchParams({ … }) for curl -d name=value commands.
Can axios ignore an invalid TLS certificate like curl -k?
Only in Node.js: pass httpsAgent: new https.Agent({ rejectUnauthorized: false }). Use it for tests against self-signed certificates, never in production. Browsers do not allow it.

Related tools