Skip to content
Json Works

Convert cURL to fetch, Axios and Python

Updated

API documentation is full of cURL commands, and the browser’s network tab can copy any request as cURL. But your code is probably JavaScript, Python or PHP. Translating by hand is easy to get wrong: a header gets dropped, a body is encoded differently, or an auth flag is forgotten. Here is how the pieces map across.

The quick way

  1. Copy the cURL command. In Chrome or Firefox, right-click a request in the Network tab and choose Copy > Copy as cURL.
  2. Paste it into the cURL Converter.
  3. Pick a target: fetch, Axios, Python requests, Node, PHP, or a structured JSON description of the request.
  4. Copy the code.

The conversion happens in your browser, so tokens and cookies in the command are not uploaded.

A worked example

curl -X POST 'https://api.example.com/v1/users' \
  -H 'Authorization: Bearer abc123' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Ada","role":"admin"}'

fetch:

const res = await fetch("https://api.example.com/v1/users", {
  method: "POST",
  headers: {
    Authorization: "Bearer abc123",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ name: "Ada", role: "admin" }),
});
const data = await res.json();

Axios:

const { data } = await axios.post(
  "https://api.example.com/v1/users",
  { name: "Ada", role: "admin" },
  { headers: { Authorization: "Bearer abc123" } }
);

Python requests:

import requests

r = requests.post(
    "https://api.example.com/v1/users",
    headers={"Authorization": "Bearer abc123"},
    json={"name": "Ada", "role": "admin"},
)
data = r.json()

Notice that Axios and requests set Content-Type: application/json for you when you pass an object, while fetch needs the header and JSON.stringify written out.

Flag-by-flag mapping

cURL flag Meaning In code
-X POST HTTP method method: "POST"
-H 'Name: value' Request header headers object
-d / --data Request body (implies POST) body / data / json
--data-urlencode Form-encoded field URLSearchParams / data= dict
-F 'file=@x.png' Multipart upload FormData / files=
-u user:pass Basic auth Authorization: Basic base64(user:pass) or auth=(user, pass)
-b 'a=1' / --cookie Send cookies Cookie header (server-side)
-L Follow redirects fetch and requests do by default
-k / --insecure Skip TLS verification verify=False in Python; avoid in production
-G Send -d data as query string Append to URL or use params
--compressed Accept gzip/br Handled automatically by most clients
-I / --head HEAD request method: "HEAD"

Details that trip people up

Body encoding. curl -d 'a=1&b=2' sends application/x-www-form-urlencoded, not JSON, unless you add the header yourself. The converter picks the right body type from the headers and content.

Quoting. The shell removes quotes before cURL sees them. A command copied from Windows cmd uses ^ for line continuation and double quotes with \" escapes, whereas bash uses \ and single quotes. Copy the variant that matches your shell, or let Chrome give you the “bash” version.

Cookies and browser-only headers. “Copy as cURL” includes everything the browser sent: Cookie, User-Agent, Sec-Fetch-*, Referer. Most of these are unnecessary. Remove them until the request breaks, then put back the last one. Browsers also forbid scripts from setting some headers (Cookie, Host, User-Agent in certain contexts), so a fetch call in the browser cannot replicate them.

CORS. A cURL command works anywhere, but the same call made with fetch from a web page is subject to CORS. A server that works fine in cURL can still be blocked in the browser. This is a server configuration issue, not a conversion bug.

Secrets. A copied command often contains live tokens. Replace them with environment variables before committing code:

headers: { Authorization: `Bearer ${process.env.API_TOKEN}` }

Tips for reliable conversions

  • Test with the cURL first. Make sure the command works in your terminal before converting. It isolates auth and network problems from code problems.
  • Check status codes. fetch does not throw on 404 or 500; check res.ok. Axios throws on non-2xx by default; requests needs r.raise_for_status().
  • Handle JSON responses. Calling .json() on an HTML error page throws; see common JSON errors.
  • Add timeouts. cURL has --max-time; fetch needs an AbortController, requests takes timeout=10.
  • Prefer the structured JSON output if you are building a tool that stores requests, since it separates method, URL, headers and body.

Going the other way

Postman can show any request as cURL, using the Code icon on the right, and can import cURL via Import > Raw text. See Postman tips: exporting responses. Once you have the response, turn it into a table or generate TypeScript types from it.

More guides

All guides · FAQ