Skip to main content

How to Convert curl Commands to JavaScript fetch

· 5 min read

API documentation almost always gives examples as curl commands, but in the browser or in Node you need them as fetch calls. The translation is mechanical once you know which curl flag maps to which fetch option, so let's walk through it.

The Basic Shape

A curl command and a fetch call carry the same information: a URL, a method, headers, and usually a body. fetch takes the URL as its first argument and an options object as its second. A bare curl of an endpoint becomes fetch of that endpoint, which defaults to a GET. Everything else is a matter of filling in that options object.

Mapping the Method

The dash-X flag sets the HTTP method. So a curl POST becomes a method property: method set to POST. If you do not pass a method, fetch uses GET, the same default curl uses. One subtlety: if a curl command has data but no explicit method flag, curl quietly switches to POST. So a command with data and no method flag is still a POST, and you must set the method explicitly in fetch.

Mapping Headers

Each dash-H flag is one header. They collect into the headers object. Two header flags for Content-Type and Accept become a headers object with those two keys. The colon-separated string splits at the first colon into key and value.

Mapping the Body

The dash-d and dash-dash-data flags carry the request body. The string after the flag becomes the body property of the options object. Note that fetch does not serialize objects for you. If you pass a JavaScript object as the body it will be stringified to a useless placeholder, so you must call JSON.stringify yourself and set the Content-Type header to application/json. For form-encoded data, the body is a query-string-style string and the Content-Type is application/x-www-form-urlencoded.

A gotcha worth remembering: when curl sees the data flag, it not only sets the body but also defaults the method to POST and the Content-Type to form-encoding. fetch does none of this automatically, so you supply all three pieces explicitly.

Mapping Authentication

The dash-u flag does HTTP Basic auth, taking a user and password pair. fetch has no equivalent shortcut; you build the header by hand. The value is the word Basic, a space, and the base64 encoding of the user-colon-password string, placed in an Authorization header. For bearer tokens, which many APIs use instead, you simply set Authorization to Bearer followed by your token directly.

Putting It Together

A full POST with JSON and a token traces every part back to a curl flag: the method from the method flag, the headers from the header flags, the body from the data flag wrapped in JSON.stringify, and the auth from a header you constructed.

Skip the Manual Work

For anything beyond a trivial command, doing this by hand is error-prone, especially with escaped quotes and multi-line commands. Paste your command into curl to fetch and it produces the equivalent JavaScript, handling the method inference, header parsing, and body wrapping for you. It runs entirely in your browser, so a command containing an API key or auth token is converted locally and never uploaded anywhere - which matters, because curl examples often have real secrets pasted right into them.

Common Pitfalls

  • Forgetting that the data flag implies POST; set the method yourself.
  • Passing an object as body instead of a JSON string.
  • Omitting the Content-Type header, so the server misreads the body.
  • Mishandling escaped quotes when copying multi-line commands.
  • Assuming fetch throws on a 404 - it does not; it only rejects on network failure, so check the response status yourself.