Convert cURL to Fetch

Turn a cURL command from API docs, Postman, or your shell history into an equivalent JavaScript fetch call, keeping the method, headers, and JSON body intact.

Open this example in cURL Converter

Open the tool, then paste the sample input below. Everything runs locally in your browser.

Open this example in cURL Converter →

The problem

Translating cURL by hand drops headers, mishandles the JSON body, or leaks shell quoting into JavaScript. The converter parses the command and produces a fetch call with the right method, headers object, and a JSON.stringify-ed body.

Sample input

cURL
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer example_token" \
  -d '{"name":"Asha","role":"admin"}'

Expected output

JavaScript fetch
await fetch("https://api.example.com/users", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer example_token"
  },
  body: JSON.stringify({
    name: "Asha",
    role: "admin"
  })
});

The JSON sent with -d becomes a JSON.stringify body, and each -H header becomes an entry in the headers object.

How to do it

  1. Paste the cURL command into the converter.
  2. Choose JavaScript Fetch output if the tool offers output options.
  3. Review the method, URL, headers, and body.
  4. Copy the generated fetch code.
  5. Replace placeholder tokens before using it in your app.

Common mistakes

  • Forgetting JSON.stringify on an object body.
  • Dropping the Content-Type header.
  • Losing the Authorization header during translation.
  • Pasting shell escaping (backslashes, single quotes) into JavaScript.
  • Embedding real tokens in front-end code.

Related tools

Related guides

FAQ

How do I convert cURL to JavaScript fetch?

Paste the cURL command into the converter and select fetch output. It maps the method, headers, and body into a fetch call.

Does fetch support request headers?

Yes. Pass a headers object in the second argument, for example { headers: { "Content-Type": "application/json" } }.

How do I send JSON with fetch?

Set Content-Type to application/json and pass body: JSON.stringify(payload).

Should I put API secrets in browser JavaScript?

No. Anything in front-end code is visible to users. Keep secrets on a server or proxy and call your own backend instead.

Why does my converted fetch request fail CORS?

CORS is enforced by the browser based on the server's response headers. Converting cURL does not change that; the API must allow your origin.

Conversion runs locally in your browser — the cURL command is parsed client-side and never uploaded.

Practical example and expected result

Turn a cURL command from API docs, Postman, or your shell history into an equivalent JavaScript fetch call, keeping the method, headers, and JSON body intact. In practice, this is most useful when you need a quick, repeatable check on a cURL command before adding it to a ticket, pull request, test fixture, or support note.

A realistic input for this workflow is curl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -H "Authorization: Bearer example_token" \ -d '{"nam.... The expected result should resemble await fetch("https://api.example.com/users", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": "Bearer examp..., with the same important values preserved.

Troubleshooting checklist

  • Confirm you copied the complete cURL command and not only a partial line or truncated preview.
  • Run the local tool once with a safe sample, then repeat with the real data only if your team policy allows it.
  • Check quoting, escaping, whitespace, encoding, timestamps, and environment-specific values before trusting the result.
  • Before sharing output, remove secrets, tokens, cookies, customer data, and production hostnames that are not needed for the review.

Next useful steps