Back to blog

    Conversion guides

    How to convert cURL to JavaScript fetch

    cURL commands convert naturally to JavaScript fetch: the URL, method, headers, and body become a fetch call and options object.

    7 min readUpdated 2026-04-28

    Basic fetch conversion

    A simple curl GET request becomes fetch(url). You can await the response and then read JSON, text, blobs, or streams depending on the API.

    JavaScript fetch
    const response = await fetch('https://api.github.com/repos/curl/curl');
    const data = await response.json();
    console.log(data);

    POST JSON with fetch

    For JSON requests, fetch uses an options object. Set method, headers, and body. Remember that fetch expects the body to be a string, so use JSON.stringify for JSON payloads.

    fetch POST JSON
    const response = await fetch('https://api.example.com/users', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name: 'Ada' }),
    });

    fetch does not throw on HTTP errors

    A common difference between curl and fetch is error behavior. fetch only rejects on network errors, not on HTTP 404 or 500 responses. Check response.ok or response.status yourself.

    Where fetch works

    fetch works in modern browsers, Node.js 18+, Deno, Bun, Cloudflare Workers, and many edge runtimes. For older Node.js versions, use node-fetch, Axios, Got, or another HTTP client.

    Try it with curlcode

    Have a curl command already? Paste it into one of the converters below to generate language-specific HTTP request code.

    Frequently asked questions

    Does fetch work in Node.js?

    Yes in Node.js 18 and newer. Older versions need a package such as node-fetch.

    How do curl headers map to fetch?

    They become properties in the headers object.

    Why does fetch not throw on 404?

    The request completed successfully at the network level. You need to check response.ok for HTTP status errors.

    Can I convert cURL to Axios instead?

    Yes. Axios is often preferred in Node.js and some frontend projects.

    Related guides