Fetch Code Generator
About this tool
About Fetch Code Generator
Generate a fetch() call you can drop straight into a browser app or a Node.js 18+ script. Fill in the method and URL, add query parameters and headers, pick Basic or Bearer authorization, and choose a body: JSON is written as JSON.stringify of a real object rather than an escaped string, URL-encoded forms are encoded correctly, and multipart uploads use FormData without a hand-set Content-Type, so the boundary is always right. The browser version reads files from an <input type="file">; the Node.js tab uses openAsBlob from node:fs and Buffer for Basic auth. The code awaits the response and prints the status and body, ready for you to switch to response.json(). Things fetch can't do are said plainly in comments, for example that browsers never let you skip TLS verification. The same request is one click away as cURL, Axios, HTTPie, Python, Go or PHP. Nothing is sent from this page.
Built and maintained by Abhi JainUpdated
How to Generate fetch() Code
- 1
Describe the request
Choose the Method, type the URL, and add Query parameters and Headers.
- 2
Add auth and body
Pick an Authorization type and a Body type and fill them in.
- 3
Copy
Use the fetch tab for browsers or Node.js for server code, then copy or download it.
Fetch Code Generator: questions and answers
How do I POST JSON with fetch?
fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }). Choose JSON under Body and the generator writes exactly this.
Why does fetch not reject on 404 or 500?
fetch only rejects on network failures. Check response.ok or response.status yourself and throw if it isn't what you expect.
Should I set Content-Type when sending FormData?
No. The browser sets multipart/form-data with the right boundary. Setting it yourself breaks the upload.
Can I use fetch in Node.js?
Yes. Node.js 18 and later include fetch, FormData and Blob globally. Use the Node.js tab for code that reads files with node:fs.
More HTTP Request Code Generator presets
Related tools
- cURL ConverterTurn a cURL command into fetch, Axios, Python, Go, PHP, Node or HTTPie code.
- Query String BuilderTurn key/value rows into an encoded query string and back again.
- HTTP Headers ReferenceSearchable reference of HTTP request and response headers with examples.
- JSON FormatterBeautify, minify and validate JSON with exact error locations, without changing a single value.