Practical developer guides

Convert cURL to fetch: preserve method, headers and body

Translate a request into browser code and check what cURL behavior cannot be reproduced in a web page.

Nav Tools ·

Identify the request parts

A cURL command combines URL, method, headers, body and client options. Start by separating protocol data from cURL-only behavior.

Use a synthetic HTTPS endpoint and body for conversion. Do not paste a production bearer token or session Cookie into an example.

curl 'https://example.com/api/items' \
  -X POST -H 'Content-Type: application/json' \
  --data-raw '{"name":"Ada"}'

Inspect the generated code

Check that the method and body survived conversion and that Content-Type describes the actual body. Form data and JSON are not interchangeable.

The converter produces source code rather than executing it. Verify the target library version and syntax before copying into the project.

fetch("https://example.com/api/items", {
  method: "POST",
  headers: {"Content-Type": "application/json"},
  body: JSON.stringify({name: "Ada"})
});

Browser restrictions still apply

Browsers control some headers and apply CORS and mixed-content policies. A working cURL call does not prove that generated fetch can read the response.

Nav Tools HTTP client requests HTTPS destinations directly without a proxy. It omits cookies and does not follow redirects; use the intended application environment for credential testing.

Compare protocol behavior

Compare the server’s received method, headers and bytes where you own the endpoint. Distinguish a code translation error from an authentication or policy failure.

Large shell commands can include expansion or unsupported flags. Review the converter result rather than assuming every command-line side effect was translated.

References

← Practical developer guides