• Web
  • HTTP
  • JavaScript

Copy as cURL, then what? 9 things that break when curl becomes code

Turning a copied curl command into fetch, Python or Go looks mechanical. It isn't: redirects, timeouts, repeated parameters, browser-only headers and shell quoting all change meaning on the way.

The utilyx team9 min read
On this page

Most API debugging ends the same way. The request works in the browser, so you open DevTools, right-click it, choose Copy as cURL, paste it into a terminal, and it works there too. Then you rewrite it as fetch() or requests.post() and something is off: a 401, a filter that's ignored, a request that never finishes.

The translation looks mechanical. A -H becomes a header, a -d becomes a body. But curl and HTTP libraries disagree about defaults in a handful of places, and each disagreement changes the request you actually send. These are the nine we hit while building our cURL converter, with what each one does and how to get curl's behaviour back.

1. Redirects: curl stops, everything else follows

Without -L, curl doesn't follow redirects. It prints the 301 or 302 response and stops. Almost every HTTP library does the opposite: fetch follows by default, Python's requests follows for every method except HEAD, Go's http.Client follows up to 10 hops, and Guzzle follows up to 5.

That matters most for writes. When a POST gets a 301, 302 or 303, a following client repeats the request to the new location as a GET without the body. So a command that answered “302 Found” in your terminal can “succeed” in code by landing on a login page. If you want to see the redirect, ask for it:

js
const res = await fetch(url, { method: "POST", body, redirect: "manual" });
python
response = requests.post(url, data=data, allow_redirects=False)

2. The same timeout means different things

-m 30 (--max-time) is a deadline for the whole transfer. The libraries start from a worse place: fetch has no timeout at all, requests waits forever unless you pass timeout=, and Go's default client never gives up. A converter that drops -m hands you code that can hang.

Even when the number carries over, it may not mean the same thing. In requests, timeout=30 limits connecting and each wait between bytes of the response, not the total. A server that trickles a byte every 20 seconds keeps the call alive indefinitely.

ClientDefault-m 30 becomesWhat it limits
fetchNo timeoutsignal: AbortSignal.timeout(30000)The whole request
Python requestsWaits forevertimeout=30Connecting, and each wait between reads
Go net/httpNever times outhttp.Client{Timeout: 30 * time.Second}The whole exchange, body included
PHP GuzzleWaits forever'timeout' => 30The whole request

3. Repeated parameters vanish in dictionaries

?tag=a&tag=b is one parameter with two values, and APIs use it for filters all the time. curl sends both. Convert it to a dictionary and the second silently wins, because a dict can't hold the same key twice:

python
# Sends only tag=b
requests.get(url, params={"tag": "a", "tag": "b"})

# Sends both
requests.get(url, params=[("tag", "a"), ("tag", "b")])

JavaScript object literals and PHP arrays behave the same way. Use new URLSearchParams([["tag", "a"], ["tag", "b"]]) in JavaScript, and a plain query string in Guzzle's 'query' option. The same applies to repeated -d tag=a -d tag=b form fields.

4. Some headers are off-limits in the browser

A command copied from DevTools carries everything the browser sent, including Cookie, Origin and Referer. Paste those into fetch on a web page and the browser quietly drops them: they're forbidden request headers, and setting one isn't an error, it just doesn't happen. The request works from curl and Node, then fails with a 401 in the browser.

In a page, let the browser attach its own cookies instead:

js
const res = await fetch(url, { credentials: "include" });

One more header to leave alone: when the body is FormData, don't set Content-Type. The browser adds it with the multipart boundary, and a hand-written value without the boundary breaks the upload.

5. @ means “read a file” — except when it doesn't

In curl, -d @body.json doesn't send the text “@body.json”. It reads the file, and with -d it also strips newlines (--data-binary @body.json sends the file byte for byte). --data-urlencode msg@note.txt reads a file too, and -F photo=@cat.png uploads one.

Two flags exist precisely to turn that off: --data-raw and --form-string send a leading @ as a literal character. A converter that treats every -d the same either sends a file name as your body or misses that a file was meant. Code can't read a file you haven't given it, so the honest output is a warning plus a body you fill in.

6. -d quietly picks the method and the content type

Any -d makes curl send a POST with Content-Type: application/x-www-form-urlencoded, unless you set a type yourself. That catches people who paste JSON with -d and no header: the server receives JSON labelled as a form, and a strict API rejects it. When you convert, decide which one you meant and say so in the headers.

-G flips it the other way: the -d values go into the query string and the request stays a GET.

bash
curl -G https://api.example.com/search -d q=curl -d per_page=5
# sends: GET /search?q=curl&per_page=5

7. Three shells, three quoting languages

The command you copied is written for one shell. In bash, single quotes are completely literal, while double quotes expand $TOKEN and backslash escapes. That's why -H "Authorization: Bearer $TOKEN" works in a terminal: the shell filled in the variable. A converter that copies the string literally sends the word $TOKEN to the API; the right translation reads the same environment variable.

On Windows, Chrome offers Copy as cURL (cmd), which escapes nearly every special character with a caret:

cmd
curl ^"https://app.example.com/api/orders?page=2^&status=open^" ^
  -H ^"accept: application/json^"

And in Windows PowerShell 5.1, curl isn't curl at all — it's an alias for Invoke-WebRequest, which takes different options. Type curl.exe to run the real thing, and use a backtick, not a backslash, to continue a line.

8. Big numbers don't survive JSON.parse

IDs from databases and payment systems often exceed 2⁵³, the largest integer a JavaScript number holds exactly. A converter that parses the JSON body and writes it back out changes the request:

js
JSON.stringify(JSON.parse('{"id":12345678901234567890}'))
// '{"id":12345678901234567000}'

Your code now asks for a different record, and nothing errors. Python's integers have no such limit, so a json= payload is safe there; in JavaScript and PHP, send the original JSON text as the body when a number is that large.

9. The token you just pasted

Copy as cURL includes everything: session cookies, Authorization headers, CSRF tokens. Paste it into code and the next git commit publishes them. Read secrets from the environment instead — process.env.API_TOKEN, os.environ["API_TOKEN"], os.Getenv("API_TOKEN") — and be careful where you paste the command in the first place. A converter that posts your command to a server has just received a working session.

A short checklist

  • No -L? Decide whether your code should follow redirects, especially after a POST.
  • Set a timeout explicitly, and check what your library's timeout actually limits.
  • Repeated query or form names need pairs, not a dictionary.
  • Running in a browser? Drop Cookie and use credentials: "include".
  • Any @ in a -d or -F value means a file your code must read.
  • JSON body without a Content-Type? curl sent it as a form.
  • $VAR in double quotes is an environment variable, not text.
  • IDs over 2⁵³ go as text in JavaScript and PHP.
  • Tokens and cookies belong in environment variables, not in the code.

Our cURL to code converter takes care of the mechanical ones: it keeps repeated parameters, reads $VARS in headers and every token or cookie from the environment, flags file references, -k and browser-blocked headers, keeps big numbers exact, carries -m over as a timeout, and understands bash, cmd and PowerShell commands. Redirects it leaves to your library's default, because only you know whether that POST should follow one. It writes fetch, axios, Python, Go and PHP, and the cURL generator goes the other way, from a form to a command quoted correctly for your shell. Everything runs in your browser tab; the command is never uploaded.

Try the cURL to code
Free, no sign-up, and it runs in your browser.
Open the tool →