cURL to Fetch Converter translates curl command-line HTTP requests into equivalent JavaScript fetch() code. Paste any curl command (including -H headers, -d data, --data-raw, -X method, -u auth, --cookie, -b) and get the equivalent fetch() call with proper headers, body, and options. Also supports reverse conversion: paste fetch() code to get the equivalent curl command for testing.
curl is the standard command-line HTTP client used for API testing, automation, and documentation. Most API documentation provides curl examples. The browser fetch() API is the modern way to make HTTP requests in JavaScript (browsers and Node.js 18+). The two use different syntax for the same operations: curl uses flags (-H for headers, -d for body, -X for method) while fetch() uses an options object with method, headers (a Headers object or plain object), and body properties.
Common curl flags and fetch equivalents: -H 'Content-Type: application/json' -> headers: {'Content-Type': 'application/json'}. -d '{"key":"val"}' -> body: JSON.stringify({key:'val'}). -X POST -> method: 'POST'. -u user:pass -> Authorization: 'Basic ' + btoa('user:pass'). --compressed -> (fetch does gzip automatically). -L (follow redirects) -> redirect: 'follow' (default in fetch). -k (insecure, skip SSL) -> no direct equivalent in browser fetch.
Simple GET request
Result: curl https://api.example.com/users -> fetch('https://api.example.com/users').then(r => r.json())
POST with JSON body
Result: curl -X POST -H 'Content-Type: application/json' -d '{"name":"Alice"}' https://api.example.com/users -> fetch(url, {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({name:'Alice'})})
With Bearer token
Result: curl -H 'Authorization: Bearer token123' https://api.example.com/me -> headers: {Authorization: 'Bearer token123'}
What is the difference between curl -d and --data-raw?
-d / --data: sends the data as the request body. If the value starts with @, it reads from a file: -d @data.json. Performs URL encoding on some characters. --data-raw: sends the exact string as-is, no special handling of @ or URL encoding. For JSON bodies, use --data-raw or wrap with single quotes: curl -d '{"key":"value"}' or curl --data-raw '{"key":"value"}'. In fetch(): both are equivalent to body: '{"key":"value"}' or body: JSON.stringify({key:'value'}).
How do I handle cookies in fetch vs curl?
curl: -b 'sessionId=abc123' sends cookies. -c cookies.txt saves response cookies to a file. -b cookies.txt reads cookies from a file. fetch(): cookies are automatically sent if: credentials: 'include' (cross-origin) or credentials: 'same-origin' (default, same-origin). For manual cookies: headers: {Cookie: 'sessionId=abc123'}. Browser fetch with credentials: 'include' requires CORS with Access-Control-Allow-Credentials: true and a non-wildcard origin. For Node.js fetch: no automatic cookie jar -- use the tough-cookie library for cookie handling.
What is the fetch API and how does it differ from XMLHttpRequest?
fetch() is the modern Promise-based API for HTTP requests, introduced in browsers around 2015 and in Node.js 18 (2022). XMLHttpRequest (XHR) is the older callback-based API used by jQuery's $.ajax and Axios internally. Key differences: fetch returns a Promise (cleaner async code). fetch does not throw on HTTP errors (4xx/5xx) -- check response.ok or response.status. fetch does not send cookies cross-origin by default (XHR does with withCredentials). fetch has no request timeout built in (use AbortController). XHR has upload progress events; fetch does not natively.
How do I set a timeout on a fetch request?
fetch() has no built-in timeout. Use AbortController: const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); const response = await fetch(url, {signal: controller.signal}); clearTimeout(timeout). The AbortController can abort any pending fetch. If the signal fires, fetch throws an AbortError. Equivalent curl flag: --max-time 5 (5-second timeout). For Node.js: the undici library (which Node's native fetch uses) supports a timeout option directly. For browsers: AbortController is the only built-in mechanism.
What curl options have no fetch equivalent?
Some curl features have no direct fetch() equivalent in browsers: -k / --insecure (skip SSL verification) -- browsers always verify SSL. --proxy (use HTTP proxy) -- browsers use system proxy settings. -v (verbose output showing headers) -- use browser DevTools Network tab instead. --limit-rate (throttle bandwidth) -- no equivalent. --max-time with millisecond precision -- AbortController is coarser. -T (file upload via PUT) -- use FormData or body: fileContent. For Node.js: some can be configured via the underlying HTTP agent. For testing with invalid SSL: use a local mock server or configure your OS trust store.