About Fetch Code Generator
Describe the request once and copy working fetch() code for the browser, Node.js 18 or later, Deno or Bun. JSON bodies become a readable object passed to JSON.stringify, form fields become URLSearchParams, and the status check that fetch leaves out is written in for you.
The code updates as you type, and notes under it explain anything it had to change: a body dropped from a GET, a Content-Type header added for JSON, or a header that browsers will not let a script set.
How it works
- The URL is checked and written out the way a browser sends it, with spaces and other unsafe characters escaped.
- Headers are read one per line. Repeated names are joined into one value, because a headers object can hold each name only once.
- A JSON body is rewritten as a JavaScript object literal with the numbers kept exactly as you typed them; a form body is passed as URLSearchParams so the encoding is done at run time.
- Everything happens in this page. Nothing is sent anywhere, including the request you describe.
Questions
Why does the code check response.ok?
fetch only rejects its promise when the request cannot be made at all. A 404 or a 500 still resolves, so without the check an error page would be treated as data. Turn the option off if you handle status codes yourself.
Where can I run the async/await version?
Top-level await works in ES modules, in the browser console, in Deno and in Node.js modules. In an older script, put the lines inside an async function or choose the .then() style.
Why did my Cookie or Host header get a warning?
Browsers protect a fixed list of headers, such as Cookie, Host, Origin and anything starting with Sec-, and silently leave them out of a request a script makes. Node.js and Deno send them, so the code is kept as written.
Does this tool send my request or my token anywhere?
No. It only writes code. Nothing you type leaves the page, and the form is not saved in the address bar, so a token in a header stays out of your history.
Everything you enter here is processed in this browser tab. Nothing is uploaded, logged or shared. How privacy works here.