How to Convert cURL to fetch, Axios, and Python
πŸŒ€

How to Convert cURL to fetch, Axios, and Python

Β· 7 min read

Share:
Try the tool: cURL ConverterOpen cURL Converter β†’

Every developer has been there: you find a perfect API example in the documentation, a Stack Overflow answer, or a Postman collection β€” and it is written as a cURL command. You need JavaScript or Python. Manually rewriting the flags, headers, and body is tedious and error-prone. The free cURL Converter turns any cURL command into clean, ready-to-run fetch, Axios, or Python requests code in a single click β€” entirely in your browser, with no account required and no data ever sent to a server.

What Is a cURL Command?

cURL (short for "Client URL") is a command-line tool that transfers data using URL syntax. It supports dozens of protocols, but HTTP and HTTPS are by far the most common use cases for developers. Because it ships with virtually every Unix-like system and is available on Windows, it has become the universal language for sharing HTTP request examples.

The flags you will see most often

Understanding the common flags makes it much easier to interpret a cURL command β€” and to verify that the converted code is correct:

FlagFull nameWhat it does
-X--requestSets the HTTP method (GET, POST, PUT, DELETE, etc.)
-H--headerAdds a request header (can be repeated)
-d--dataSends a request body (implies POST if no -X is given)
--data-rawβ€”Like -d but does not process @ file references
-u--userSets Basic Auth credentials (username:password)
-b--cookieSends a cookie string
-L--locationFollows redirects
--compressedβ€”Requests gzip compression

A typical cURL command reads left-to-right: the method, then headers, then body, then the URL. Once you recognise the pattern, the conversion logic becomes intuitive β€” which is exactly what the converter automates.

How to Convert cURL to fetch (Step by Step)

Step 1 β€” Copy the cURL command

Grab the cURL command from wherever it lives: browser DevTools (right-click a network request β†’ "Copy as cURL"), API documentation, a colleague's Slack message, or a README.

Step 2 β€” Paste it into the converter

Open the cURL Converter, paste the command into the input field, and choose your target language: fetch (JavaScript), Axios, or Python requests.

Step 3 β€” Copy the generated code

The tool instantly renders the equivalent code. Click the copy button and paste it directly into your project. No reformatting needed.

That is the entire workflow. The converter handles quoting edge cases, multiline bodies, repeated headers, and auth schemes automatically β€” details that trip up manual translation.

Before and After: Real Conversion Examples

Example 1 β€” Authenticated POST with a JSON body

cURL input:

curl -X POST https://api.example.com/v1/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.TOKEN" \
  -d '{"product_id": 42, "quantity": 3}'

fetch output (JavaScript):

const response = await fetch("https://api.example.com/v1/orders", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer eyJhbGciOiJIUzI1NiJ9.TOKEN",
  },
  body: JSON.stringify({ product_id: 42, quantity: 3 }),
});

const data = await response.json();

Axios output (JavaScript):

const { data } = await axios.post(
  "https://api.example.com/v1/orders",
  { product_id: 42, quantity: 3 },
  {
    headers: {
      Authorization: "Bearer eyJhbGciOiJIUzI1NiJ9.TOKEN",
    },
  }
);

Python requests output:

import requests

response = requests.post(
    "https://api.example.com/v1/orders",
    json={"product_id": 42, "quantity": 3},
    headers={"Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.TOKEN"},
)
data = response.json()

Notice how the converter intelligently maps -d with a JSON content type to JSON.stringify in fetch, to Axios's second positional argument, and to Python's json= keyword β€” rather than the raw body or data= string form.

Example 2 β€” Basic Auth GET request

cURL input:

curl -u admin:s3cr3t https://api.example.com/users

fetch output:

const response = await fetch("https://api.example.com/users", {
  headers: {
    Authorization: "Basic " + btoa("admin:s3cr3t"),
  },
});

Python requests output:

response = requests.get(
    "https://api.example.com/users",
    auth=("admin", "s3cr3t"),
)

Headers, POST Bodies, and Auth β€” What to Watch For

Headers

Every -H flag becomes a key-value pair in the headers object. When converting to JavaScript, remember that header names are case-insensitive by spec but some frameworks (like Express or Next.js route handlers) may read them in lowercase. The converter preserves the original casing from the cURL command.

POST bodies and Content-Type

The Content-Type header is the most important flag to check after conversion. Common scenarios:

  • application/json β€” the body should be JSON-stringified in fetch; use json= in Python requests.
  • application/x-www-form-urlencoded β€” use URLSearchParams in fetch or data= with a dict in Python.
  • multipart/form-data β€” use FormData in the browser or files= in Python. cURL generates a boundary automatically with -F; the converted code must do the same.

Authentication

cURL flagEquivalent in fetch / AxiosPython requests
-u user:passbtoa() + Authorization: Basic headerauth=("user", "pass")
-H "Authorization: Bearer TOKEN"Header passed directlyHeader passed directly
-b "session=abc"Cookie headercookies={"session": "abc"}

Tips and Best Practices After Converting

  1. Strip credentials before sharing. cURL commands copied from DevTools often contain live session cookies or API keys. Replace them with placeholders before committing code or pasting into a chat.

  2. Add error handling. The generated code is a clean starting point, not production-ready. Wrap fetch calls in try/catch, check response.ok, and handle non-2xx status codes explicitly.

  3. Verify the Content-Type. If the original cURL command sends application/json but you change the body structure, update the header too.

  4. Use async/await consistently. The converter outputs modern async syntax. Avoid mixing it with .then() chains in the same function β€” it makes the control flow harder to reason about.

  5. Test against a real endpoint. Paste the converted code into a sandbox like CodePen, a Node.js REPL, or a Python script and fire it against the API before integrating it into your codebase.

Common Mistakes When Converting cURL Manually

  • Forgetting the method. If cURL uses -d without -X, it defaults to POST. Many developers forget this and emit a GET request in their JavaScript code instead.
  • Double-encoding the body. Calling JSON.stringify on a string that is already a JSON string produces '"{\\"key\\":\\"value\\"}"' β€” a stringified string, not an object. Always start from a plain JavaScript object.
  • Missing the Content-Type header. fetch does not add Content-Type: application/json automatically, even when you pass a JSON string as the body. You must set it explicitly β€” unlike Axios, which infers it.
  • Ignoring --compressed. This flag tells cURL to request and decompress gzip responses. Browsers and the Node.js fetch API handle decompression transparently, so the flag can be dropped β€” but in older environments you may need to handle it.
  • Copying multiline cURL without joining the lines. Backslash line continuations are a shell feature. If you paste a multiline command into a JavaScript string or Python code, the backslashes will break it. Always pass the full command as a single logical string to the converter.

Frequently Asked Questions

What languages does the cURL Converter support?

The tool currently converts to three targets: fetch (vanilla JavaScript, works in the browser and Node.js 18+), Axios (the popular HTTP client library), and Python requests (the standard HTTP library for Python). These cover the vast majority of day-to-day API integration work.

Does the converter send my cURL command to a server?

No. The cURL Converter runs entirely client-side in your browser. Your cURL commands β€” which may contain API keys, tokens, or other sensitive values β€” are never transmitted anywhere. This is by design, and it means the tool works offline once the page has loaded.

Can it handle very long or complex cURL commands?

Yes. The parser handles multi-line commands (shell backslash continuation), repeated -H flags, --data-raw, --data-urlencode, -F for multipart forms, and cookie strings. If you encounter a command that produces unexpected output, double-check that the shell backslashes have been removed and the entire command is on a single line in the input field.

I got a cURL command from browser DevTools β€” will it work?

Absolutely. "Copy as cURL" from Chrome, Firefox, or Safari DevTools is one of the most common sources for cURL commands. These commands can be verbose β€” they often include a dozen or more headers β€” but the converter handles all of them and produces tidy, readable output.

Is there a difference between curl to fetch and curl to Axios for a simple GET?

For a simple GET request with no custom headers, both outputs are nearly identical in length and behavior. Axios starts to shine for complex scenarios: it automatically serialises JSON bodies, throws on non-2xx responses by default, and provides a cleaner interceptor API. fetch is the built-in, dependency-free choice. The converter lets you generate both and choose the one that fits your project's existing stack.

Conclusion

Converting cURL commands to JavaScript or Python by hand is repetitive, error-prone, and a poor use of your time. Whether you are integrating a third-party API, prototyping in a Jupyter notebook, or migrating a backend client to a new language, the free cURL Converter eliminates the busywork β€” no account, no installation, no data leaving your machine. Paste your command, pick your language, and get back to building.

Found this useful? Share it with your team:

Share:

Ready to use cURL Converter?

It is free, requires no signup, and runs entirely in your browser.

Open the cURL Converter