From curl to JavaScript fetch()
The Fetch API is built into every modern browser and into Node.js 18 and later, so it is the natural target when turning curl examples into JavaScript. This curl to JavaScript converter generates clean fetch() code with await, a status check and a response parser, ready to drop into your app.
Example
curl -X POST 'https://api.example.com/v1/orders?notify=true' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_TOKEN' \
-d '{"sku":"LAMP-01","qty":2}' \
--max-time 30const response = await fetch('https://api.example.com/v1/orders?notify=true', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer YOUR_API_TOKEN',
},
body: JSON.stringify({
"sku": "LAMP-01",
"qty": 2
}),
signal: AbortSignal.timeout(30000),
});
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
const data = await response.text();
console.log(data);A JSON body is detected and written as JSON.stringify(…), which is easier to edit than an escaped string. When the request sends an Accept: application/json header, the code reads the response with response.json() instead of response.text().
How curl features translate
- Form uploads (
-F) become aFormDataobject. File fields referencefileInput.files[0], since browser code cannot read arbitrary paths from disk. - Basic auth (
-u) becomes anAuthorization: Basicheader built withbtoa(). - Timeouts (
-m) useAbortSignal.timeout(). - Redirects – fetch follows redirects by default, like
curl -L.
What fetch cannot do
Some curl options have no fetch equivalent, and the converter adds a comment instead of silently dropping them:
-k(skip certificate checks) – not possible in fetch; certificate validation stays on.-x(proxy) – browsers do not let pages configure a proxy.- Manual
Cookieheaders – browsers ignore them; usecredentials: 'include'for the site's own cookies (a manual header does work in Node.js).
CORS: why it works in curl but not in the browser
curl talks to any server directly. A browser page may only read responses from another origin if that server allows it with CORS headers such as Access-Control-Allow-Origin. If the generated code fails in the browser with a CORS error while curl works, the API is not configured for browser access. Run the code in Node.js or call the API from your own back end instead. Never put secret API keys in front-end code that users can inspect.
Supported curl options
| curl option | Meaning |
|---|---|
-X, --request | HTTP method (GET, POST, PUT, PATCH, DELETE…) |
-H, --header | Request headers |
-d, --data, --data-raw, --data-binary, --data-urlencode | Request body; implies POST |
--json | JSON body with Content-Type and Accept headers |
-F, --form | Multipart form fields and file uploads |
-u, --user and --oauth2-bearer | Basic and bearer authentication |
-b, --cookie, -A, -e | Cookies, user agent and referer |
-G, --get | Send -d data as query string |
-L, -k, -m, -x, -I, -T | Follow redirects, skip TLS checks, timeout, proxy, HEAD request, upload file |
Line continuations (\), single and double quotes are parsed like a Unix shell. Options that only affect curl's own output, such as -s, -v or -o, are ignored silently; any other unsupported option is listed as a comment at the top of the generated code, so nothing is dropped without notice.
Prefer another language? Try cURL to Python or cURL to PHP. You can also test the request interactively with the API tester. Conversion runs locally in your browser.