Reading:
Converting a curl Command to Working Code: JavaScript, Python, PHP, Go

Converting a curl Command to Working Code: JavaScript, Python, PHP, Go

Metamug

//: # ()

Have a curl command you need turned into real code right now? Use our free cURL to Code Converter — paste it and get working JavaScript, Python, PHP, or Go, instantly in your browser.

Why curl commands are everywhere

Almost every API's documentation shows a curl example, because curl is the universal, copy-pasteable way to demonstrate an HTTP request without assuming any particular programming language. Browser DevTools follow the same convention — right-click any request in the Network tab, "Copy as cURL," and you get a runnable command that reproduces exactly what the browser sent, headers and all.

The problem: you rarely want to run curl itself in production. You want that same request expressed as code, in whatever language your app is written in — and translating flags by hand is repetitive and easy to get subtly wrong (especially escaping quotes inside a JSON body).

Reading a curl command

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

Breaking down the flags that matter most:

Flag Meaning
-X, --request HTTP method (GET, POST, PUT, DELETE, ...). If omitted, curl uses GET, or POST automatically if -d is present.
-H, --header Adds one request header. Repeatable — one -H per header.
-d, --data, --data-raw Request body. Using -d also implies POST if no -X was given.
-u, --user HTTP Basic Auth credentials (user:pass), sent as a base64-encoded Authorization header.
-b, --cookie Sends a Cookie header.
-A, --user-agent Sets the User-Agent header.
-G, --get Forces GET and turns any -d data into query string parameters instead of a body.

The trailing \ at the end of each line is just a shell line-continuation — it means "this is still one command," which is why pasted multi-line curl commands (like the ones DevTools generates) need that continuation stripped before tokenizing, if you're writing your own parser.

The translation isn't always 1:1

A few things that trip people up when hand-converting:

  • Headers matter for body encoding. A Content-Type: application/json header means the body should be sent as-is (already JSON text). Forgetting to set it — or a library auto-setting a different one — is the most common cause of "works in curl, fails in code."
  • Quoting differences. The JSON body in curl is usually single-quoted to avoid the shell interpreting " characters. When that same string becomes a language string literal (a JS/Python/Go double-quoted string, or a PHP string), every embedded " needs re-escaping for that language's syntax — and PHP double-quoted strings additionally interpret $ as variable interpolation, which will silently corrupt a JSON body containing a $ (like an OData query with $filter=) unless you either escape it or use PHP single quotes instead.
  • Default method inference. curl -d '...' https://... with no -X sends a POST — a detail that's easy to drop when translating by hand into a client library that defaults to GET.

Try it

Paste any curl command — including the multi-line kind copied straight from browser DevTools — into the cURL to Code Converter and get correctly escaped, working request code in JavaScript, Python, PHP, or Go.



Icon For Arrow-up
Comments

Post a comment