While formdata-node ships with its own File and Blob implementations, these might eventually be removed in favour of Node.js' Blob (introduced in v14.18) and File (when it will be introduced). In order to help you smoothen that transition period, our own Blob and File , as well as FormData itself, provides support Blob objects created by Node.js' implementation.
You can install this package with npm:
npm install formdata-node
yarn add formdata-node
pnpm add formdata-node
This package is build for and bundled for both ESM and CommonJS, so you can use it in both environments.
import FormData> from "formdata-node" // I assume Got >= 12.x is used for this example import got from "got" const form = new FormData() form.set("greeting", "Hello, World!") const data = await got.post("https://httpbin.org/post", body: form>).json() console.log(data.form.greeting) // => Hello, World!
import Readable> from "stream" import FormDataEncoder> from "form-data-encoder" import FormData> from "formdata-node" // Note that `node-fetch` >= 3.x have builtin support for spec-compliant FormData, sou you'll only need the `form-data-encoder` if you use `node-fetch` import fetch from "node-fetch" const form = new FormData() form.set("field", "Some value") const encoder = new FormDataEncoder(form) const options = method: "post", headers: encoder.headers, body: Readable.from(encoder) > await fetch("https://httpbin.org/post", options)
import FormData, File> from "formdata-node" // You can use `File` from fetch-blob >= 3.x import fetch from "node-fetch" const form = new FormData() const file = new File(["My hovercraft is full of eels"], "file.txt") form.set("file", file) await fetch("https://httpbin.org/post", method: "post", body: form>)
import FormData, Blob> from "formdata-node" // You can use `Blob` from fetch-blob const form = new FormData() const blob = new Blob(["Some content"], type: "text/plain">) form.set("blob", blob) // Will always be returned as `File` let file = form.get("blob") // The created file has "blob" as the name by default console.log(file.name) // -> blob // To change that, you need to set filename argument manually form.set("file", blob, "some-file.txt") file = form.get("file") console.log(file.name) // -> some-file.txt
import FormData, Blob> from "formdata-node" import Blob as FetchBlob> from "fetch-blob" const input = new FetchBlob(["a", "b", "c"]) const blob = new Blob([input]) // Accepts 3rd party blobs as BlobParts await blob.text() // -> abc const form = new FormData() form.set("file", input) const file = form.get("file") // -> File await file.text() // -> abc
import Blob as NodeBlob> from "node:buffer" import FormData, Blob> from "formdata-node" const input = new NodeBlob(["a", "b", "c"]) const blob = new Blob([input]) // Accepts Node.js' Blob implementation as BlobParts await blob.text() // -> abc const form = new FormData() form.set("file", input) const file = form.get("file") // -> File await file.text() // -> abc
import fileFromPath> from "formdata-node/file-from-path" import FormData> from "formdata-node" import fetch from "node-fetch" const form = new FormData() form.set("file", await fileFromPath("/path/to/a/file")) await fetch("https://httpbin.org/post", method: "post", body: form>)
import Readable> from "stream" import FormData> from "formdata-node" class BlobFromStream #stream constructor(stream, size) this.#stream = stream this.size = size > stream() return this.#stream > get [Symbol.toStringTag]() return "Blob" > > const content = Buffer.from("Stream content") const stream = new Readable( read() this.push(content) this.push(null) > >) const form = new FormData() form.set("stream", new BlobFromStream(stream, content.length), "file.txt") await fetch("https://httpbin.org/post", method: "post", body: form>)
import Readable> from "stream" import Encoder> from "form-data-encoder" import FormData> from "formdata-node" const form = new FormData() // You can use file-shaped or blob-shaped objects as FormData value instead of creating separate class form.set("stream", type: "text/plain", name: "file.txt", [Symbol.toStringTag]: "File", stream() return getStreamFromSomewhere() > >) const encoder = new Encoder(form) const options = method: "post", headers: "content-type": encoder.contentType >, body: Readable.from(encoder) > await fetch("https://httpbin.org/post", method: "post", body: form>)
formdata-node | formdata-polyfill | undici FormData | form-data | |
---|---|---|---|---|
.append() | ✔️ | ✔️ | ✔️ | ✔️ 1 |
.set() | ✔️ | ✔️ | ✔️ | ❌ |
.get() | ✔️ | ✔️ | ✔️ | ❌ |
.getAll() | ✔️ | ✔️ | ✔️ | ❌ |
.forEach() | ✔️ | ✔️ | ✔️ | ❌ |
.keys() | ✔️ | ✔️ | ✔️ | ❌ |
.values() | ✔️ | ✔️ | ✔️ | ❌ |
.entries() | ✔️ | ✔️ | ✔️ | ❌ |
Symbol.iterator | ✔️ | ✔️ | ✔️ | ❌ |
ESM | ✔️ | ✔️ | ✔️ 2 | ✔️ 2 |
Blob | ✔️ 3 | ✔️ 4 | ✔️ 3 | ❌ |
Browser polyfill | ❌ | ✔️ | ✔️ | ❌ |
Builtin encoder | ❌ | ✔️ | ✔️ 5 | ✔️ |
1 Does not support Blob and File in entry value, but allows streams and Buffer (which is not spec-compliant, however).
2 Can be imported in ESM, because Node.js support for CJS modules in ESM context, but it does not have ESM entry point.
3 Have builtin implementations of Blob and/or File, allows native Blob and File as entry value.
4 Support Blob and File via fetch-blob package, allows native Blob and File as entry value.
5 Have multipart/form-data encoder as part of their fetch implementation.
✔️ - For FormData methods, indicates that the method is present and spec-compliant. For features, shows its presence.
❌ - Indicates that method or feature is not implemented.
Creates a new FormData instance.
Set a new value for an existing key inside FormData, or add the new field if it does not already exist.
Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.
The difference between set() and append() is that if the specified key already exists, set() will overwrite all existing values with the new one, whereas append() will append the new value onto the end of the existing set of values.
Returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.
Returns all the values associated with a given key from within a FormData object.
Returns a boolean stating whether a FormData object contains a certain key.
Deletes a key and its value(s) from a FormData object.
Executes a given callback for each field of the FormData instance
Returns an iterator allowing to go through all keys contained in this FormData object. Each key is a string .
Returns an iterator allowing to go through all values contained in this object FormData object. Each value is a FormDataValue .
Returns an iterator allowing to go through key/value pairs contained in this FormData object. The key of each pair is a string; the value is a FormDataValue .
The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data.
Creates a new Blob instance. The Blob constructor accepts following arguments:
Returns the MIME type of the Blob or File .
Returns the size of the Blob or File in bytes.
Creates and returns a new Blob object which contains data from a subset of the blob on which it's called.
Returns a ReadableStream which upon reading returns the data contained within the Blob .
Returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer .
Returns a Promise that resolves with a string containing the contents of the blob, interpreted as UTF-8.
The File class provides information about files. The File class inherits Blob .
Creates a new File instance. The File constructor accepts following arguments:
Available from formdata-node/file-from-path subpath.
Creates a File referencing the one on a disk by given path.
Available from formdata-node/file-from-path subpath.
Creates a File referencing the one on a disk by given path. Synchronous version of the fileFromPath .
Available from formdata-node/file-from-path subpath.
Checks if given value is a File, Blob or file-look-a-like object.