cURL to fetch converter
Paste a curl command, for example one copied from your browser's network tab or an API doc, and get the same request as fetch code. Choose a plain call, async/await or a promise chain.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Paste the curl command. A leading
curl or $ curl is fine, and so are multi-line commands ending in \.
- Pick the code style.
- Copy the fetch code. Anything that could not be converted is listed under the result.
How it works
The command is split into words the way a shell does. Options are read in order: the method comes from -X, otherwise it is HEAD for -I, POST when there is a body or form, and GET otherwise. Several -d values are joined with &, and a body from -d gets the application/x-www-form-urlencoded content type unless you gave one. With -G the data is moved onto the URL.
Example
curl -u ann:secret https://api.example.com/me
becomes
const response = await fetch('https://api.example.com/me', {
headers: {
'Authorization': 'Basic YW5uOnNlY3JldA==',
},
});
const text = await response.text();
Tips
The code reads the body as text. Replace response.text() with response.json() when the API returns JSON. For file uploads, the generated code uses fileInput.files[0] as a stand-in: point it at your file input or a Blob.
Frequently asked questions
Which curl options are converted?
-X/--request, -H/--header, -d/--data/--data-raw/--data-binary/--data-ascii/--data-urlencode, --json, -u/--user, -F/--form, -G, -I, -A, -b and -e, and the URL (also with --url). Quotes with ', " and $'…' and backslash line continuations are understood.
What is ignored?
Options that only change how curl behaves, such as -L, -s, -S, -k, -v, -i and --compressed, are dropped without a note. fetch already follows redirects. Options this tool does not know, and files (-d @file or -F name=@file), are listed under the result as not converted.
What does -u user:pass become?
An Authorization header with Basic and the Base64 of user:pass, calculated for you. Anyone who sees the code can decode it, so replace secrets with variables before sharing.
Why does the request fail in the browser when it works in curl?
Browsers apply CORS, which curl does not, and they refuse to set some headers such as Cookie and Host. For a request from a page, the server must allow your origin. In Node.js or Deno fetch is not limited by CORS.
What does --json do?
It sends the text as the body with POST, and adds Content-Type and Accept headers set to application/json unless you set them yourself, like curl does.