Convert cURL to JavaScript fetch
Paste a curl command to get fetch() code that works in the browser, Deno and Bun. Pick Node.js in the converter for server-side code.
AI DevRuns in your browser — nothing uploaded
How curl options map to fetch()
fetch is built into every browser, so there’s nothing to install — but it has fewer options than curl, and some things have to be written out by hand.
- Headers go in the
headersobject. fetch doesn’t add a Content-Type for a string body, so the converter keeps the one from the command. - A JSON body becomes
body: JSON.stringify({...})— fetch has no json option. -dform fields becomenew URLSearchParams({...}), and fetch sets the urlencoded Content-Type for you.-Fbecomes aFormDataobject. Don’t set Content-Type yourself: fetch adds it with the multipart boundary.-u user:passbecomes anAuthorization: Basic …header, already base64-encoded, because fetch has no auth option.--max-timebecomessignal: AbortSignal.timeout(ms).
A request that works in curl can still fail in the browser. The API has to allow your site’s origin (CORS), and browsers don’t let scripts set some headers, such as Cookie and Host. For server-side code, choose Node.js in the converter instead.
Example: curl to JavaScript fetch
This command, pasted into the converter above, gives the code below.
curl 'https://api.example.com/v1/projects?page=2&per_page=50' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY'const response = await fetch("https://api.example.com/v1/projects?page=2&per_page=50", {
headers: {
Accept: "application/json",
Authorization: "Bearer YOUR_API_KEY",
},
});
console.log(response.status);
console.log(await response.text());How it works
- 1
Reads the command like a shell
Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.
- 2
Understands curl’s options
-X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.
- 3
Writes idiomatic code
json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.
- 4
Tested against real curl
Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.
Questions, answered
curl isn’t a browser, so CORS doesn’t apply to it. In a browser, the API must send an Access-Control-Allow-Origin header that allows your site. If it doesn’t, call the API from your own server (for example with the Node.js output) and fetch from there.
No. fetch only rejects when the request can’t be made at all — a network error, a blocked CORS request or a timeout. Check response.ok (true for 200–299) or response.status yourself.
Use const data = await response.json() instead of response.text(). It throws if the body isn’t valid JSON, so check response.ok first.
Choose axios in the converter. It works in the browser and Node, throws on error statuses and has options for auth, params and timeouts.
Yes, Node 18 and later have fetch built in. The Node.js option is a better fit there, though: it adds a timeout, builds the auth header with Buffer, reads files for uploads and supports proxies and -k.
Popular uses
Read the guide
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- Regex ExplainerPaste a regex, get plain English plus test cases.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev