About this tool
Describe the request once and get modern async/await fetch code that sets the method, headers and body, checks response.ok and reads the result. JSON bodies become a readable object passed to JSON.stringify, form bodies use URLSearchParams and multipart bodies use FormData. A Node.js variant is one menu choice away and reads upload files with fs.openAsBlob. Everything is generated locally.
How to use fetch() Code Generator
- Set the method and URL.
- Add headers, authentication and a body.
- Choose JavaScript fetch or Node.js fetch in the code menu.
- Copy the snippet into your project.
Frequently asked questions
Why doesn't my form request include a Content-Type header?
For URLSearchParams and FormData bodies, fetch sets the correct Content-Type automatically, including the multipart boundary. Setting it yourself would break multipart uploads.
Does the code throw on 404 or 500 responses?
fetch itself only rejects on network errors, so the snippet checks response.ok and throws with the status code when the server returns an error.
Will the browser version work against any API?
Only if the API allows your site through CORS. Server-side code in Node.js is not subject to CORS.