Upload files and form fields using multipart/form-data encoding.
Code
Utilitiesconst formData = new FormData();
formData.append('username', 'john');
formData.append('email', '[email protected]');
formData.append('file', new Blob(['Hello World'], { type: 'text/plain' }), 'hello.txt');
const response = await fetch('https://httpbin.org/post', {
method: 'POST',
body: formData
});
await response.json();Browser·fetch() may be limited by CORS
Notes
- Don't set
Content-Typeheader manually - browser sets it with correct boundary - Use
new Blob()to create file-like data or pass actualFileobjects - httpbin.org echoes back the form fields and files received
More JavaScript Snippets
Add Query Parameter
Add or update a query parameter in a URL string.
Bearer Token Authentication
Make an authenticated HTTP request using Bearer token for JWT or OAuth.
Check Cloudflare Cache Status
Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.
CORS Cross-Origin Request
Make a cross-origin HTTP request with CORS headers inspection.
Delayed Response (Test Timeouts)
Request a delayed response to test timeout handling with AbortController.
Download Binary File
Download an image or binary file and convert it to a blob or base64.