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:

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

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.

Try the cURL → fetch Converter →