API docs show requests as curl commands, and browser DevTools will copy any request as one. Sooner or later that command has to become real code. Most of the translation is mechanical, but curl, Python and JavaScript don’t share the same defaults, and that’s where a request that worked in the terminal starts failing in your app.
This guide maps the common curl flags to Python requests, JavaScript fetch and axios, then covers the differences that a straight translation won’t fix for you.
Getting the curl command
- Chrome or Edge: open DevTools, go to the Network tab, right-click a request and choose Copy → Copy as cURL.
- Firefox: the same right-click menu in the Network tab, under Copy Value.
- Postman: open the request, click the code icon (
</>) and pick cURL.
A command copied from the browser usually contains your session cookies and any auth tokens. Treat it like a password: don’t paste it into a ticket or a chat, and remove those values before committing the code that comes out of it.
What each curl flag becomes
| curl | Python requests | fetch | axios |
|---|---|---|---|
-X POST | requests.post | method: "POST" | method: "post" |
-H 'Name: value' | headers={...} | headers: {...} | headers: {...} |
-d 'a=1&b=2' | data={...} | body: URLSearchParams | data: URLSearchParams |
-d '{...}' with a JSON header | json={...} | body: JSON.stringify() | data: {...} |
-G with -d | params={...} | Query string in the URL | params: {...} |
-F file=@photo.jpg | files={"file": open()} | body: FormData | data: FormData |
-u user:pass | auth=("user", "pass") | Authorization: Basic header | auth: {...} |
--max-time 10 | timeout=10 | signal: AbortSignal.timeout() | timeout: 10000 |
-k | verify=False | No equivalent | httpsAgent (Node only) |
One request, three languages
Here’s a typical JSON POST:
curl -X POST https://api.example.com/v1/orders \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"sku": "TSHIRT-M", "qty": 2}'Python requests
import requests
response = requests.post(
"https://api.example.com/v1/orders",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"sku": "TSHIRT-M", "qty": 2},
timeout=30,
)
print(response.status_code)
print(response.text)The Content-Type header has gone because json= sets it for you. If you use data= with a dict instead, requests sends form fields, not JSON. That swap is one of the most common bugs in hand-converted code.
JavaScript fetch
const response = await fetch("https://api.example.com/v1/orders", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({ sku: "TSHIRT-M", qty: 2 }),
});
console.log(response.status);
console.log(await response.text());fetch does nothing to the body for you. Pass an object without JSON.stringify and it sends the text [object Object]. Leave out the header and the server sees text/plain.
axios
import axios from "axios";
const response = await axios({
method: "post",
url: "https://api.example.com/v1/orders",
headers: { Authorization: "Bearer YOUR_API_KEY" },
data: { sku: "TSHIRT-M", qty: 2 },
});
console.log(response.status);
console.log(response.data);axios serialises the object and sets the JSON header itself, and response.data is already parsed.
The defaults that change when you leave curl
| Behaviour | curl | requests | fetch | axios |
|---|---|---|---|---|
| Follows redirects | Only with -L | Yes | Yes | Yes |
| A 404 or 500 counts as a failure | Only with -f | Only with raise_for_status() | No, check response.ok | Yes, it throws |
| Time limit | None | None | None | None |
| Subject to CORS | No | No | Yes, in a browser | Yes, in a browser |
Three of these cause most of the trouble:
- Errors. A fetch to a URL that returns 500 doesn’t throw. If your code goes straight to
response.json(), the failure shows up later as a confusing parse error. Checkresponse.okfirst. axios works the other way round: a 404 lands in yourcatchblock, with the response onerror.response. - Time limits. In a terminal you can press Ctrl+C. In a server or a scheduled job, a request with no timeout can hang for as long as the connection stays open, so set one.
- CORS. curl can call any URL. Browser code can only read a response from another origin if that server allows it, and adding an
Authorizationheader makes the browser send a preflightOPTIONSrequest first. If the same call works from curl and fails in the browser with a CORS error, no change to your request will fix it. The server has to allow your origin, or the call has to go through your own backend. That’s also where a secret API key belongs, since anything in browser code can be read by whoever opens the page.
Browsers also refuse to let scripts set some headers, including Cookie, Host and Content-Length, so a command copied from DevTools won’t work as-is in browser code if it relies on those. Node doesn’t have the same limit on Cookie.
What curl’s -d really does
Most conversion mistakes come from this one flag. -d switches the method to POST, and unless you say otherwise it labels the body application/x-www-form-urlencoded, even when the body is JSON:
# The server is told this is form data, not JSON
curl https://api.example.com/v1/orders -d '{"qty": 2}'
# The server is told this is JSON
curl https://api.example.com/v1/orders \
-H 'Content-Type: application/json' \
-d '{"qty": 2}'If an API rejects the first version, the header is the problem, not your JSON. Since version 7.82, curl has a shortcut that sets it for you: --json '{"qty": 2}' sends the body with both Content-Type and Accept set to application/json.
Three more details worth knowing:
-d @body.txtreads the body from a file but removes its line breaks. Use--data-binary @body.txtto send the file exactly as it is.-Gmoves everything from-dinto the query string and makes the request a GET.curl -G https://api.example.com/search -d limit=20asks for/search?limit=20.-ddoesn’t encode anything. To send a value with spaces or an&in it, use--data-urlencode 'q=blue shirts'.
When curl works and your code doesn’t
If the status code or response differs between the two, go through these in order:
- Compare what’s actually sent. Point both at an echo service such as
https://httpbin.org/anything, which returns the request it received, and compare the two. Don’t send real tokens to a third-party service: swap in a dummy value first. Adding-vto the curl command also prints the headers it sends. - Check for expired values. Cookies and tokens copied from a browser stop working when the session ends, so a command from yesterday can return 401 in any language.
- Check the User-Agent. Each client announces itself, and
python-requests/x.yis blocked by some sites that letcurl/x.ythrough. Copy the header from the command that worked. - Check for a redirect. Code that follows a redirect can behave differently from curl, which stops at it. Print the final URL, or run curl with
-L. - Check the body’s type. Look for form data where JSON was expected, or the reverse. It’s the
-dproblem above, and it often appears as a 400 or 415.
Convert a command without doing it by hand
Paste any command into the free cURL to Code converter and it writes the request in Python, JavaScript fetch, Node.js, axios, Go, PHP or Java. It reads Chrome’s Copy as cURL output for both bash and Windows, drops browser-only headers like sec-fetch-mode, and warns you when a body looks like JSON but curl would label it as form data. The conversion happens in your browser, so the command and any tokens in it aren’t uploaded.
There are also pages with worked examples for each language, including curl to Python requests, curl to fetch and curl to axios.