Skip to main content
DevConverter
Home/Network/cURL ↔ Fetch Converter

cURL ↔ Fetch Converter

Convert cURL commands to JavaScript fetch code and vice versa.

About this tool

cURL is a command-line tool and library for transferring data with URLs, available on virtually every operating system. It is the de facto standard for demonstrating HTTP requests in API documentation, tutorials, and developer forums because its flags map directly and legibly to HTTP concepts: -H 'Header: Value' sets a request header, -d '{...}' or --data sends a request body, -X POST/PUT/PATCH/DELETE sets the HTTP method, -u user:pass sets credentials, and --json is a shorthand introduced in cURL 7.82 that sets both Content-Type and Accept to application/json.

The Fetch API is the modern standard for making HTTP requests in JavaScript, available natively in all modern browsers and in Node.js 18+. It uses Promises and async/await, accepts a URL and an optional options object, and returns a Response object. Fetch is lower-level than libraries like axios — it does not automatically parse JSON bodies or throw on 4xx/5xx responses, but these behaviors are easy to add and the explicitness gives you complete control over the request and response handling.

The mapping from cURL to Fetch covers most common cases. A -H 'Content-Type: application/json' flag becomes an entry in the headers object. A -d '{}' or --data-raw '{}' flag becomes the body option. The -X flag sets the method option. cURL's -u username:password performs HTTP Basic Authentication, which in Fetch becomes an Authorization header with the value Basic base64(username:password). The --compressed flag tells cURL to request gzip encoding — Fetch handles this automatically.

Several cURL flags have no direct Fetch equivalent. The --cookie and --cookie-jar flags for managing cookie files translate to fetch's credentials option (include, same-origin, or omit), which controls whether the browser's cookie jar is used but does not support file-based cookie storage. The --resolve and --connect-to flags for overriding DNS resolution have no browser Fetch equivalent. The -k or --insecure flag to skip TLS certificate verification also has no Fetch equivalent — browsers enforce TLS validation unconditionally.

When adapting cURL examples from API documentation to production JavaScript code, remember to handle errors explicitly. Fetch does not reject on HTTP error status codes (4xx, 5xx) — it only rejects on network failures. Add a check like if (!response.ok) throw new Error(response.status) after the await fetch() call. Also consider using AbortController to add request timeouts, since Fetch has no built-in timeout option unlike cURL's --max-time flag.