curl to Code: Convert curl Commands into Python, JavaScript, and Go
Turn any curl command into idiomatic Python requests, JS fetch, or Go net/http. Copy curl from DevTools, handle headers and JSON bodies, and paste working code.
curl to Code: Convert curl Commands into Python, JavaScript, and Go
Every API reference I open hands me a curl command. Stripe, OpenAI, GitHub — they all assume curl is the lingua franca, and it mostly is. The trouble starts the moment I need that same request inside an actual project. Retyping six headers, hand-escaping a JSON body, and getting the basic-auth syntax right for whatever language I happen to be in is the kind of tedious work that breeds typos. Converting curl to code by hand is where bugs sneak in.
This guide walks through how I take a curl command and turn it into clean HTTP code in Python, JavaScript, and Go — where the curl usually comes from, how headers and request bodies translate, and the few places the languages disagree.
Where your curl command comes from
There are two common sources, and they behave differently.
The first is API documentation. These snippets are written for a bash or zsh shell: single-quoted arguments, -H for each header, and a \ at the end of each line to continue onto the next. They paste cleanly because they were authored to be readable.
The second is your browser. Open DevTools, go to the Network tab, right-click any request, and choose Copy → Copy as cURL. This captures the exact request your page made — every header, cookie, and body byte. It's the fastest way to reproduce something the browser already did. One caveat worth knowing per the Chromium and Firefox DevTools docs: "Copy as cURL" includes your live session cookies and Authorization header, so anything you generate from it carries real credentials. Strip those before sharing.
A third, messier source is a Windows cmd shell, which uses ^ for line continuation and double quotes everywhere. Those snippets often mis-tokenize against bash-style parsers, so if a conversion looks scrambled, re-copy from a bash context.
A real conversion: curl to Python requests
Here is a representative command — a POST to an API with a bearer token and a JSON body:
curl -X POST https://api.example.com/v1/messages \
-H "Authorization: Bearer sk-test-abc123" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4","max_tokens":256,"stream":false}'
Run it through a converter targeting Python requests and you get:
import requests
response = requests.post(
"https://api.example.com/v1/messages",
headers={
"Authorization": "Bearer sk-test-abc123",
"Content-Type": "application/json",
},
json={
"model": "gpt-4",
"max_tokens": 256,
"stream": False,
},
)
print(response.status_code)
Three details matter here. The -X POST becomes requests.post(...). Each -H becomes a key in the headers dict. And because the Content-Type declares JSON and the body parses as valid JSON, the body is emitted as a json= dict rather than a raw string — note false became Python's False. That JSON-to-language mapping (true/false/null → True/False/None) is the single most error-prone thing to do by hand, and the part a generator earns its keep on.
The curl to Code converter does exactly this parse locally in your browser, then lets you flip the target between Python, JS fetch, Node axios, PHP, and Go without re-pasting.
Headers, bodies, and auth across languages
The same curl command produces three structurally different idioms.
JavaScript fetch keeps headers as a plain object and serializes the body with JSON.stringify:
const response = await fetch("https://api.example.com/v1/messages", {
method: "POST",
headers: {
"Authorization": "Bearer sk-test-abc123",
"Content-Type": "application/json",
},
body: JSON.stringify({ model: "gpt-4", max_tokens: 256, stream: false }),
});
Go net/http needs a strings.NewReader for the body and explicit header Set calls:
body := strings.NewReader(`{"model":"gpt-4","max_tokens":256,"stream":false}`)
req, _ := http.NewRequest("POST", "https://api.example.com/v1/messages", body)
req.Header.Set("Authorization", "Bearer sk-test-abc123")
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
Authentication is where hand-conversion goes wrong most often. A -u user:pass should never become a hand-built base64 string. Each language has a native mechanism: auth=(user, pass) in Python requests, an auth: { username, password } config in axios, and req.SetBasicAuth(...) in Go. A bearer Authorization header, by contrast, passes through unchanged into the headers object of every target — no special handling needed.
For the request body, the rule is consistent: if a JSON Content-Type is present and the body parses, it gets pretty-printed into the language's native structure; otherwise it stays a raw escaped string. Form data from -F becomes multipart fields, and multiple -d values join with & exactly the way curl itself does.
Inspect before you convert
A long curl command is hard to read at a glance. Before generating code, it helps to break out what the request actually does: the method, the real host, how many headers it sets, and the decoded query string. That last part is easy to overlook — a URL like ?page=2&sort=desc&filter=active packs three params into one line.
When I want to read those parameters cleanly, I lean on a dedicated URL query params extractor to split them into a table. If you need that query string as a JSON object for a config file, query string to JSON does the structured version, and JSON formatter pretty-prints a minified body so you can actually see its shape before pasting it into code.
A small workflow that sticks
My loop now looks like this. I hit a flaky endpoint with a one-off curl in the terminal, add a header, tweak the body, and run it again until it returns 200. That final curl is the request that works — so instead of reconstructing it from shell history by memory, I paste it into a converter, pick fetch or Go, and the generated code mirrors the exact method, headers, and body that succeeded. The reconstruction step, where I used to introduce a wrong header casing or a dropped query param, is gone.
It also smooths handoffs. A backend engineer shares a curl that hits an internal service; the frontend dev needs it as fetch. Drop it in, copy the output, done — no back-and-forth about exact header casing.
Wrapping up
Converting curl to code is really three small problems: parsing a shell-quoted command correctly, mapping options to each language's HTTP idiom, and translating a JSON body into native types. Doing all three by hand is where the typos live. Copy the curl from your API docs or from DevTools "Copy as cURL", run it through the curl to Code converter, and paste working Python, JavaScript, or Go straight into your project. Just remember the one privacy rule: if the command carries a token or password, copy the generated code by hand rather than sharing the URL, since the curl text is encoded into the share link.
Made by Toolora · Updated 2026-06-13