How to use the curl to fetch tool
- Paste one curl command into the curl command box. Multi-line commands with trailing backslashes are fine.
- Or select Example to load a POST request that has a JSON body.
- Select Generate fetch code to translate the command.
- Check the method, headers and body in the Result box.
- Select Copy and paste the code into an async function, an ES module or the browser console.
How the curl to fetch conversion works
The tool first splits the command roughly the way a shell would. Single and double quotes group words. Inside double quotes, ordinary backslashes in paths are preserved. Outside single quotes, a trailing backslash joins the next line. Shell variables such as $TOKEN are not expanded and appear literally in the output. The tool then walks the arguments and collects the URL, the method, the headers and the data.
The method comes from -X or --request. If none is given, the result uses POST when the command has data and GET otherwise, which mirrors curl. Several -d options are joined with an ampersand, again as curl does. Repeated headers retain all their values. A GET or HEAD command with a body produces an error because fetch cannot send that combination.
Worked example: PUT with headers and a JSON body
The command below spans four lines. The converter joins them, keeps both headers in order and passes the JSON string through as the body.
curl -X PUT https://api.example.com/v1/users/7 \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer TOKEN' \
-d '{"role":"admin"}'
const response = await fetch("https://api.example.com/v1/users/7", {
"method": "PUT",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer TOKEN"
},
"body": "{\"role\":\"admin\"}"
});
console.log(await response.text());Supported curl options
Only the options listed here are translated. Any other argument stops the conversion with Unsupported curl argument and the name of the flag.
Options without an entry in the list include -u, -L, -F, -G, -A, -b, -k, --json, --data-urlencode and --compressed. Short flags must be separated from their value, so write -X POST, not -XPOST. Pipes, redirects, semicolons and an unquoted ampersand are treated as shell operators and rejected, so quote any URL that contains a query string.
- -X and --request for the HTTP method.
- -H and --header in Name: Value form. A header without a colon is rejected.
- -d, --data and --data-binary with a literal value. A value starting with @ would read a file in curl and is refused.
- --data-raw, which keeps a leading @ as plain text.
- --url, or a bare argument that starts with http:// or https://.
Differences between curl and fetch to check by hand
The generated code translates what was typed, not everything curl does implicitly. Review these points before you ship it.
- For literal form data, the converter adds application/x-www-form-urlencoded as curl does. A Content-Type supplied in the command takes precedence.
- CORS. In a browser, a fetch to another origin needs the server to allow it. curl has no such restriction.
- Restricted headers. Browsers do not let scripts set headers such as Cookie, Host and Origin, so those entries are dropped there.
- Redirects. fetch follows redirects by default, while curl needs -L.
- Bodies on GET. A command that forces -X GET together with data produces a GET with a body, which fetch refuses to send.
- Top-level await. The generated code uses await, so run it inside an async function or a module.
Frequently asked questions
Does this tool send the request?
No. It only parses the text and prints code. No network call is made to the URL in your command, and the command itself, including any tokens in its headers, stays in your browser.
Why do I get Unsupported curl argument?
The command contains a flag outside the supported set, such as -u, -L or --compressed, or a combined form such as -XPOST. For basic authentication, replace -u with an explicit Authorization header.
Why does the converted request fail in the browser but work in curl?
The usual reason is CORS: the API does not allow requests from your page's origin. Other causes are headers the browser will not let scripts set and a missing Content-Type on a form or JSON body.
Does the output work in Node.js?
Yes. Node.js 18 and later include a global fetch, and the generated code uses only standard options. Wrap it in an async function if your file uses CommonJS.