How to Convert cURL Commands to JavaScript Fetch, Axios, and Python Requests
Translate terminal cURL flags (-X, -H, -d, --data-raw, -u) into clean, production-ready JavaScript fetch(), Axios, and Python requests code.
Nearly every API documentation portal—from Stripe and OpenAI to Cloudflare and GitHub—publishes reference examples as curl terminal commands. Furthermore, Chrome and Firefox DevTools allow you to right-click any network call and choose Copy as cURL. Knowing how each cURL flag maps to fetch() and Python requests saves hours of manual header re-typing.
1. Quick Translation Dictionary: cURL Flags to Code
| cURL Flag | JavaScript fetch() Equivalent | Python requests Equivalent |
|---|---|---|
| -X POST | method: 'POST' | requests.post(url, ...) |
| -H "Key: Value" | headers: { 'Key': 'Value' } | headers={'Key': 'Value'} |
| -d '{"a":1}' | body: JSON.stringify({ a: 1 }) | json={'a': 1} |
| -u user:pass | Authorization: 'Basic ' + btoa(...) | auth=('user', 'pass') |
2. Complete Side-by-Side Translation Example
Suppose you copied the following authenticated POST command from an API reference or Chrome DevTools Network inspector:
curl -X POST "https://api.example.com/v1/charges" \
-H "Authorization: Bearer sk_test_51Nz..." \
-H "Content-Type: application/json" \
--data-raw '{"amount": 2500, "currency": "usd"}'
In modern TypeScript / JavaScript with native fetch(), always check response.ok explicitly because fetch() only rejects on network failures, not on HTTP 4xx or 5xx responses:
const response = await fetch('https://api.example.com/v1/charges', {
method: 'POST',
headers: {
'Authorization': 'Bearer sk_test_51Nz...',
'Content-Type': 'application/json'
},
body: JSON.stringify({ amount: 2500, currency: 'usd' })
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
const payload = await response.json();
3. Common Pitfalls When Converting Terminal cURL to Browser Code
- Never Expose Secret Keys in Frontend Bundles: If a cURL snippet includes a secret API key (
sk_live_...), run the translated code inside a backend server route or serverless function rather than browser client code. - CORS Preflight Headers: Terminal cURL ignores browser Cross-Origin Resource Sharing (CORS) policies, whereas browser
fetch()sends anOPTIONSpreflight whenever you attach custom headers likeAuthorizationorContent-Type: application/json.
Paste any multi-line cURL snippet directly into our cURL to Code Converter to generate ready-to-run Fetch, Axios, or Python snippets instantaneously.
Frequently Asked Questions
What does the --data-raw flag mean when copying a request as cURL from Chrome DevTools?
Chrome uses --data-raw instead of -d so that an @ symbol in your payload (such as an email address) is sent literally as text rather than interpreted by cURL as a local file path to read.
How does cURL -u username:password translate to HTTP headers in fetch()?
The -u flag generates a Basic Authentication header by Base64-encoding the "username:password" string: Authorization: "Basic " + btoa("username:password").