How to Convert a cURL Command to JavaScript fetch()
Updated 2026-06-21
To convert a curl command to JavaScript fetch(), map the URL to the first fetch() argument and move everything else — the method, headers, and request body — into an options object as the second argument. Each curl flag has a direct fetch() equivalent, so the translation is mechanical once you know the mapping.
How curl flags map to fetch()
curl and fetch() describe the same HTTP request with different syntax. Here is how the common flags line up:
- -X or --request sets the HTTP method, which becomes the method property (GET, POST, PUT, DELETE, and so on). If no -X is present and there is a body, the method is POST.
- -H or --header adds a request header. Each one becomes a key-value pair inside the headers object.
- -d, --data, or --data-raw sends a request body, which becomes the body property. Sending data also implies a POST unless you override it.
- -u or --user is HTTP basic auth and maps to an Authorization header.
- --compressed, -L, and -k describe transport behavior that fetch() handles automatically or through its own options.
The URL itself — the only argument without a flag — goes in as the first parameter to fetch().
A worked example
Take this command: curl -X POST https://api.example.com/login -H "Content-Type: application/json" -d '{"user":"sam"}'
It becomes a fetch() call to https://api.example.com/login with an options object containing method set to POST, a headers object holding the Content-Type, and body set to the JSON string. Because fetch() does not parse objects for you, the body stays a string and you set the Content-Type header yourself — exactly what the original -d and -H flags did.
The result drops straight into browser code, a Node script (Node 18+ ships fetch built in), or a serverless function.
Common pitfalls to watch
- Quotes and escaping. A header value or JSON body with nested quotes is the easiest thing to mistype by hand. Converting from the original command avoids that.
- The body is not auto-stringified. fetch() will not turn a JavaScript object into JSON for you — you pass a string, which is what curl sent anyway.
- Cookies and credentials. A curl request relying on -b cookies or session auth needs the credentials option in fetch() to behave the same way in the browser.
- Multipart and file uploads (curl -F) map to a FormData object rather than a plain string body.
Convert yours instantly
Rather than translate flag by flag, paste your full command into the cURL → fetch Converter and get a clean, ready-to-paste fetch() snippet in a second. It runs entirely in your browser — your command and any tokens or headers inside it never leave your machine and are never uploaded. Paste, convert, and copy the result into your code. Try the cURL → fetch Converter.