formdata
The FormData API provides a way to build multipart/form-data bodies for HTTP requests, typically used with fetch for file uploads and form submissions.
API Surface
Class: FormData
new FormData()— create empty form.form.append(name, value[, filename])— add field or file.form.set(name, value[, filename])— set/replace field.form.get(name),form.getAll(name)— retrieve values.form.has(name),form.delete(name)— check/remove.- Iterable:
for (const [key, value] of form).
Examples (English only)
// Build a form body
const form = new FormData();
form.append("username", "mamun");
form.append("avatar", new Blob(["filedata"]), "avatar.png");
// Submit with fetch
const res = await fetch("https://example.com/upload", {
method: "POST",
body: form
});
console.log("status:", res.status);
// Iterate form data
for (const [key, value] of form) {
console.log(key, value);
}
Notes
- Automatically sets Content-Type: multipart/form-data with proper boundary when used with fetch.
- Supports files (Blob, File, Buffer) and simple fields.
- Duplicate keys are allowed; use
.getAll()to retrieve them. - Useful for browser and server-side environments.