Convert cURL Commands to fetch, axios, and Python requests

You copy a curl command from an API's documentation, a Postman export, or your browser's DevTools — and now you need it as actual application code. Translating curl flags into fetch, axios, or Python requests by hand is repetitive and easy to get wrong, especially with escaped quotes and multi-line headers. This guide breaks down what each curl flag means, then converts the same real command into all three languages side by side.

Why cURL Commands Show Up Everywhere

curl is the universal language of "here's how to call this API." You'll run into it in three places constantly:

  • API documentation "try it" examples — most public APIs (Stripe, GitHub, Razorpay) show a curl example first because it works in any terminal with zero setup
  • Postman "Copy as cURL" — when you build a request in Postman and want to share or script it, exporting as curl is the fastest path
  • Browser DevTools "Copy as cURL" — right-click any request in the Network tab and copy it as curl to reproduce it outside the browser

In every case, the next step is usually the same: turn that curl command into code that fits into your actual application.

Common cURL Flags and What They Map To

  • -X, --request — the HTTP method (GET, POST, PUT, DELETE). Maps directly to method in fetch/axios, or the function name (requests.post(...)) in Python.
  • -H, --header — one HTTP header per flag. Maps to entries in a headers object/dict.
  • -d, --data — the request body. Maps to body in fetch, data in axios, and json= or data= in Python requests.
  • -u, --user — basic auth credentials in user:password form. Maps to an Authorization: Basic ... header or a dedicated auth parameter.
  • -G, --get — forces data supplied with -d to be sent as URL query parameters instead of a body.

Worked Example: The Same Request in curl, fetch, axios, and Python

Here is a realistic curl command for creating a user via a POST request with a bearer token:

curl -X POST https://api.example.com/v1/users \
  -H "Authorization: Bearer sk_live_abc123" \
  -H "Content-Type: application/json" \
  -d '{"name": "Ravi Kumar", "email": "ravi@example.com"}'

JavaScript — fetch

const response = await fetch('https://api.example.com/v1/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer sk_live_abc123',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'Ravi Kumar',
    email: 'ravi@example.com',
  }),
});

const data = await response.json();
console.log(data);

JavaScript — axios

const axios = require('axios');

const response = await axios.post(
  'https://api.example.com/v1/users',
  { name: 'Ravi Kumar', email: 'ravi@example.com' },
  {
    headers: {
      Authorization: 'Bearer sk_live_abc123',
      'Content-Type': 'application/json',
    },
  }
);

console.log(response.data);

Python — requests

import requests

response = requests.post(
    "https://api.example.com/v1/users",
    headers={
        "Authorization": "Bearer sk_live_abc123",
        "Content-Type": "application/json",
    },
    json={"name": "Ravi Kumar", "email": "ravi@example.com"},
)

print(response.json())

Notice the Python version uses json= instead of manually setting the body and Content-Type header — requests handles JSON serialization and the header for you automatically, which is a common source of confusion when converting by hand.

Choosing fetch vs axios vs requests

  • fetch — built into every modern browser and Node.js 18+, no dependency to install. Good default for small frontend projects or quick scripts, though you handle JSON parsing and error checking (fetch does not reject on 4xx/5xx) yourself.
  • axios — a popular library for larger JavaScript/Node projects. Automatically parses JSON, rejects the promise on error status codes, and supports interceptors for things like attaching auth tokens globally. Worth the dependency once a project makes more than a handful of API calls.
  • Python requests — the standard choice for Python scripts, data pipelines, and backend services. Simple, well-documented API with built-in support for sessions, retries, and authentication helpers.

There's no universally "correct" choice — pick based on what the rest of your codebase already uses, and keep it consistent across a project so error handling stays predictable.

Common Pitfalls When Converting by Hand

  • Forgetting Content-Type — fetch and axios don't set application/json automatically; omit it and many APIs will reject or misparse the body
  • Double-encoding JSON — passing an already-stringified body to axios's data (which stringifies for you) can produce a double-escaped payload
  • Losing query parameters — curl commands with -G -d "key=value" send data as query params, not a body; missing this turns a GET-with-filters into a broken POST
  • Mishandling escaped quotes — curl commands copied from Windows PowerShell often use different quoting than bash, which trips up manual conversion

Frequently Asked Questions

How do I convert a curl command to JavaScript fetch?

Map -X to the method property, each -H header to the headers object, and -d to aJSON.stringify(...) body. Make sure a Content-Type header is set if the body is JSON.

Is there a free tool to convert curl to code automatically?

Yes. Dev Brains AI cURL to Code Converter takes any pasted curl command and instantly outputs JavaScript fetch, axios, and Python requests code — free, no signup required.

Should I use fetch, axios, or Python requests?

Use fetch for small frontend projects with no extra dependency needed. Use axios for larger JavaScript/Node projects that benefit from automatic JSON parsing and interceptors. Use Python requests for scripts, pipelines, and Python backend services.

Try the Free cURL to Code Converter

Paste a curl command and get instant fetch, axios, and Python requests code. No signup, no cost.

Related articles