Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
formdata-node
Advanced tools
The formdata-node package is a Node.js module that allows you to create, manipulate, and encode multipart/form-data streams. It can be used to submit forms and upload files via HTTP requests in a way that is compatible with browser FormData API.
Creating FormData
This feature allows you to create a new FormData instance and append fields to it, similar to how you would in a browser environment.
const { FormData } = require('formdata-node');
const formData = new FormData();
formData.append('key', 'value');
Appending Files
This feature enables appending files to the FormData instance, which can then be used to upload files to a server.
const { FormData, fileFromPath } = require('formdata-node');
const formData = new FormData();
formData.append('file', fileFromPath('./path/to/file.txt'));
Retrieving FormData Content
This feature allows you to iterate over the entries in the FormData object, enabling you to access the keys and values that have been appended.
const { FormData } = require('formdata-node');
const formData = new FormData();
formData.append('key', 'value');
for (const [key, value] of formData) {
console.log(key, value);
}
Encoding FormData
This feature is used to encode the FormData into a Blob, which can then be used to send the data over an HTTP request.
const { FormData, formDataToBlob } = require('formdata-node');
const formData = new FormData();
formData.append('key', 'value');
const blob = formDataToBlob(formData);
The 'form-data' package is similar to 'formdata-node' and is used to create readable 'multipart/form-data' streams. It can be used in the Node.js environment and is compatible with 'request' library. It differs in API and internal implementation but serves a similar purpose.
Busboy is a Node.js module for parsing incoming HTML form data. It's different from 'formdata-node' as it focuses on parsing form data rather than creating it, but it can handle multipart/form-data, which is commonly used for file uploads.
Multiparty is a Node.js module for parsing multipart/form-data requests, which is used for uploading files. Unlike 'formdata-node', which is for creating and encoding form data, 'multiparty' is designed for the server-side to parse incoming data.
FormData implementation for Node.js
You can install this package with npm:
npm install formdata-node
Or yarn:
yarn add formdata-node
Or pnpm
pnpm add formdata-node
This package has its own encoder that allows to read the content from the FormData
instances into the multipart/form-data
format. Just use Symbol.asyncIterator
method to get async iterator or just access FormData#stream
property to get Readable
stream. Note that in the next major release both of this methods will be removed in favour of form-data-encoder
, which is basically the reimplementation of builtin formdata-node
encoder that was separated from this package to allow to re-use it in different HTTP clients or use it to add some additional logic into the encoding process. See form-data-encoder
documentation to get more information.
import {FormData} from "formdata-node"
import got from "got"
const fd = new FormData()
fd.set("greeting", "Hello, World!")
const options = {
body: fd.stream, // Set internal stream as request body
headers: fd.headers // Set headers of the current FormData instance
}
got.post("https://httpbin.org/post", options)
.then(res => console.log("Res: ", res.body))
.catch(err => console.error("Error: ", err))
Readable.from()
:import {Readable} from "stream"
import {FormData} from "formdata-node"
import fetch from "node-fetch"
const fd = new FormData()
fd.set("field", "Some value")
const options = {
method: "post",
headers: fd.headers,
body: Readable.from(fd)
}
await fetch("https://httpbin.org/post", options)
import {Readable} from "stream"
import {Encoder} from "form-data-encoder"
import {FormData} from "formdata-node"
import fetch from "node-fetch"
const fd = new FormData()
fd.set("field", "Some value")
const encoder = new Encoder(fd)
const options = {
method: "post",
headers: encoder.headers,
body: Readable.from(encoder)
}
await fetch("https://httpbin.org/post", options)
import {createReadStream} from "fs"
import {FormData} from "formdata-node"
// I assume that there's node-fetch@3 is used for this example since it has formdata-node support out of the box
// Note that they still in beta.
import fetch from "node-fetch"
const fd = new FormData()
fd.set("file", createReadStream("/path/to/a/file"))
// Just like that, you can send a file with formdata-node
await fetch("https://httpbin.org/post", {method: "post", body: fd})
fileFromPath
or fileFromPathSync
helpers. It does the same thing as fetch-blob/from
, but returns a File
instead of Blob
:import {FormData, fileFromPath} from "formdata-node"
import fetch from "node-fetch"
const fd = new FormData()
fd.set("file", await fileFromPath("/path/to/a/file"))
await fetch("https://httpbin.org/post", {method: "post", body: fd})
Note that this method is preferable over the fs.createReadStream()
and will be the only option (along with its async version) in next major release.
File
object that inherits Blob
from fetch-blob
package:import {FormData, File} from "formdata-node"
import fetch from "node-fetch"
const fd = new FormData()
const file = new File(["My hovercraft is full of eels"], "hovercraft.txt")
fd.set("file", file)
await fetch("https://httpbin.org/post", {method: "post", body: fd})
import {FormData} from "formdata-node"
import Blob from "fetch-blob"
const fd = new FormData()
const blob = new Blob(["Some content"], {type: "text/plain"})
fd.set("blob", blob)
// Will always be returned as `File`
let file = fd.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
fd.set("file", blob, "some-file.txt")
file = fd.get("file")
console.log(file.name) // -> some-file.txt
class FormData
constructor([entries]) -> {FormData}
Creates a new FormData instance
boundary -> {string}
Returns a boundary string of the current FormData
instance. Read-only property.
stream -> {stream.Readable}
Deprecated! This property will be removed in 4.x version. Use form-data-encoder
to encode FormData into multipart/form-data
format in case if your HTTP client does not support spec-compatible FormData implementations.
Returns an internal Readable stream. Use it to send queries, but don't push anything into it. Read-only property.
headers -> {object}
Deprecated! This property will be removed in 4.x version. Use form-data-encoder
to encode FormData into multipart/form-data
format in case if your HTTP client does not support spec-compatible FormData implementations.
Returns object with Content-Type
header. Read-only property.
set(name, value[, filename, options]) -> {void}
Set a new value for an existing key inside FormData, or add the new field if it does not already exist.
null
and undefined
),
Buffer
, ReadStream
, Blob
or File
.
Note that Arrays and Object will be converted to string by using String function.Buffer
, File
, Blob
and ReadStream
. You can set it either from and argument or options.Buffer
, File
, Blob
and ReadStream
. You can set it either from and argument or options.MIME
) of the file represented by a File
object.append(name, value[, filename, options]) -> {void}
Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.
null
and undefined
),
Buffer
, ReadStream
, Blob
or File
.
Note that Arrays and Object will be converted to string by using String function.Buffer
, File
, Blob
and ReadStream
. You can set it either from and argument or options.Buffer
, File
, Blob
and ReadStream
. You can set it either from and argument or options.MIME
) of the file represented by a File
object.get(name) -> {string | File}
Returns the first value associated with the given name.
If the field has Blob
, Buffer
, File
or ReadStream
value, the File-like object will be returned.
getAll(name) -> {Array<string | File>}
Returns all the values associated with a given key from within a FormData object.
If the field has Blob
, Buffer
, File
or ReadStream
value, the File-like object will be returned.
has(name) -> {boolean}
Check if a field with the given name exists inside FormData.
delete(name) -> {void}
Deletes a key and its value(s) from a FormData
object.
getComputedLength() -> {number}
Deprecated! This property will be removed in 4.x version. Use form-data-encoder
to encode FormData into multipart/form-data
format in case if your HTTP client does not support spec-compatible FormData implementations.
Returns computed length of the FormData content.
forEach(callback[, ctx]) -> {void}
Executes a given callback for each field of the FormData instance
keys() -> {Generator<string>}
Returns an iterator
allowing to go through the FormData keys
values() -> {Generator<string | File>}
Returns an iterator
allowing to go through the FormData values
entries() -> {Generator<[string, string | File]>}
Returns an iterator
allowing to go through the FormData key/value pairs
[Symbol.iterator]() -> {Generator<[string, string | File]>}
An alias for FormData#entries()
[Symbol.asyncIterator]() -> {AsyncGenerator<Buffer>}
Deprecated! This property will be removed in 4.x version. Use form-data-encoder
to encode FormData into multipart/form-data
format in case if your HTTP client does not support spec-compatible FormData implementations.
Returns an async iterator allowing to read form-data body using for-await-of syntax.
Read the async iteration proposal
to get more info about async iterators.
class File extends Blob
constructor(blobParts, filename[, options]) -> {File}
The File
class provides information about files. The File
object inherits Blob
from fetch-blob
package.
MIME
) of the file represented by a File
object.fileFromPath(path[, filename, options]) -> {Promise<File>}
Creates a File
referencing the one on a disk by given path.
File
constructor. If not presented, the file path will be used to get it.MIME
) of the file represented by a File
object.fileFromPathSync(path[, filename, options]) -> {File}
Creates a File
referencing the one on a disk by given path. Synchronous version of the fileFromPath
.
File
constructor. If not presented, the file path will be used to get it.MIME
) of the file represented by a File
object.isFileLike(value) -> {boolean}
Check if given value is a File, Blob or file-look-a-like object.
FormData
documentation on MDNFile
documentation on MDNBlob
documentation on MDNformdata-polyfill
HTML5 FormData
for Browsers & NodeJS.fetch-blob
a Blob implementation on node.js, originally from node-fetch.form-data-encoder
- Encoder for multipart/form-datathen-busboy
a promise-based wrapper around Busboy. Process multipart/form-data content and returns it as a single object. Will be helpful to handle your data on the server-side applications.@octetstream/object-to-form-data
converts JavaScript object to FormData.FAQs
Spec-compliant FormData implementation for Node.js
The npm package formdata-node receives a total of 1,402,943 weekly downloads. As such, formdata-node popularity was classified as popular.
We found that formdata-node demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.