cURL to Code
Convert curl commands to code (JS fetch, Python requests, etc.).Runs in this tab.⌘ ↵ runs it
How to use cURL to Code
- Paste a curl command into cURL Command. A line ending in a backslash is joined with the next line. DevTools "Copy as cURL" is the usual source.
- Pick a tab: JS Fetch, Python, PHP, or Node Axios.
- Copy the generated code. Unknown flags are ignored, and the first token that is not a flag is the URL.
A curl command in four languages
The page opens on this command:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer token123" \
-d '{"name": "John", "email": "john@example.com"}'
A trailing backslash joins the next line. JS Fetch writes:
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123',
},
body: JSON.stringify({"name": "John", "email": "john@example.com"}),
redirect: 'manual',
});
const data = await response.json();
console.log(data);
redirect: 'manual' is there because the sample has no -L. Fetch follows redirects unless the option says otherwise, so a command without -L gets manual. Python writes:
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer token123',
}
json_data = {"name": "John", "email": "john@example.com"}
response = requests.post('https://api.example.com/users', headers=headers, json=json_data, allow_redirects=False)
print(response.json())
A DevTools GET
Copy as cURL for a GET with one auth header:
curl 'https://example.com/dashboard' -H 'Authorization: Bearer token123'
JS Fetch:
const response = await fetch('https://example.com/dashboard', {
headers: {
'Authorization': 'Bearer token123',
},
redirect: 'manual',
});
const data = await response.json();
console.log(data);
-b 'session=abc123' is copied as a Cookie header. -H 'Cookie: ...' does the same.
Flags
| Flag | What the generator does |
|---|---|
-X, --request |
Sets the method |
-H, --header |
Copies the header |
-d, --data, --data-raw, --data-binary |
Sets the body, and switches GET to POST |
-u, --user |
Basic auth. Fetch puts btoa(...) on the Authorization header. Python uses auth=(). PHP uses CURLOPT_USERPWD. Axios uses auth |
-A, --user-agent |
Sets the User-Agent header |
-b, --cookie |
Sets a Cookie header |
-L, --location |
Follow redirects. Fetch omits redirect: 'manual'. Python omits allow_redirects=False. PHP sets CURLOPT_FOLLOWLOCATION. Axios omits maxRedirects: 0 |
-k, --insecure |
Python gets verify=False. PHP gets CURLOPT_SSL_VERIFYPEER set to false. Fetch has no way to skip TLS, so -k does not appear in that tab. Axios does not emit it either |
curl -L -k https://example.com/old on the JS Fetch tab is a plain fetch('https://example.com/old') plus response.json(). The Python tab is requests.get('https://example.com/old', verify=False).
Unknown flags are ignored. The first token that is not a flag is the URL. -F, --data-urlencode, and cookie jars are not parsed.
The four tabs are JS Fetch, Python, PHP, and Node Axios. curlconverter.com and Trillworks cover more languages, including Go, Java, and C#.
Next step
Turn a click-through into an interactive demo
AtriumKit records your web app once in Chrome and turns every click into a step of a demo you can embed or share. There’s a free plan.
Try AtriumKit freeQuestions
How do I convert curl to fetch?
Is this curlconverter?
Which curl flags are converted?
Comments
More in Web & Networking
- URL Parser
- HTTP Status Codes Reference
- HTTP Headers Reference
- MIME Types Reference
- HTTP Methods Reference
- IP Address Converter
- CIDR Calculator
- DNS Record Types Reference
Comments