Socket
Socket
Sign inDemoInstall

formdata-node

Package Overview
Dependencies
4
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

formdata-node


Version published
Maintainers
1
Created

Package description

What is formdata-node?

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.

What are formdata-node's main functionalities?

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);

Other packages similar to formdata-node

Readme

Source

FormData

FormData implementation for Node.js. Built over Readable stream and async generators. Can be used to communicate between servers with multipart/form-data format.

dependencies Status devDependencies Status Build Status Code Coverage

Installation

You can install this package from npm:

npm install --save formdata-node

Or with yarn:

yarn add formdata-node

API

constructor FormData([fields])

Initialize new FormData instance

  • {array} [fields = null] – an optional FormData initial fields. Each initial field should be passed as an array with 3 elemtnts as a collection of the objects with "name", "value" and "filename" props. See the FormData#append for more info about the available format.
Instance properties
get boundary() -> {string}

Returns a boundary string of the current FormData instance.

get stream() -> {stream.Readable}

Returns an internal Readable stream.

Instance methods
set(name, value[, filename]) -> {void}

Set a new value for an existing key inside FormData, or add the new field if it does not already exist.

  • {string} name – The name of the field whose data is contained in value
  • {any} value – The field value. You can pass any JavaScript primitive type (including null and undefined), Buffer or Readable stream. Note that Arrays and Object will be converted to string by using String function.
  • {string} [filename = undefined] – A filename of given field. Can be added only for Buffer and Readable .
append(name, value[, filename]) -> {void}

Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.

  • {string} name – The name of the field whose data is contained in value
  • {any} value – The field value. You can pass any JavaScript primitive type (including null and undefined), Buffer or Readable stream. Note that Arrays and Object will be converted to string by using String function.
  • {string} [filename = undefined] – A filename of given field. Can be added only for Buffer and Readable .
get(name) -> {string | Buffer | stream.Readable}

Returns the first value associated with the given name. Buffer and Readable values will be returned as-is.

  • {string} name – A name of the value you want to retrieve.
getAll(name) -> {string[] | Buffer[] | stream.Readable[]}

Returns all the values associated with a given key from within a FormData object.

  • {string} name – A name of the value you want to retrieve.
has(name) -> {boolean}

Check if a field with the given name exists inside FormData.

  • {string} – A name of the field you want to test for.
delete(name) -> {void}

Deletes a key and its value(s) from a FormData object.

  • {string} name – The name of the key you want to delete.
forEach(callback[, ctx]) -> {void}

Executes a given callback for each field of the FormData instance

  • {function} callback – Function to execute for each element, taking three arguments:
    • {any} value – A value(s) of the current field.
    • {string} name – Name of the current field.
    • {FormData} fd – The FormData instance that forEach is being applied to
  • {any} [ctx = null] – Value to use as this context when executing the given callback
keys() -> {iterator}

Returns an iterator allowing to go through the FormData keys

values() -> {iterator}

Returns an iterator allowing to go through the FormData values

entries() -> {iterator}

Returns an iterator allowing to go through the FormData key/value pairs

[Symbol.iterator]() -> {iterator}

An alias of FormData#entries

[Symbol.asyncIterator]() -> {asyncIterator}

Returns an async iterator which allows to read the data from internal Readable stream using for-await syntax. Read the async iteration proposal for more info about async generator functions.

  • then-busboy is 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.
  • FormData interface documentation on MDN

Keywords

FAQs

Last updated on 01 Mar 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc