Socket
Socket
Sign inDemoInstall

form-data

Package Overview
Dependencies
4
Maintainers
5
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    form-data

A module to create readable `"multipart/form-data"` streams. Can be used to submit forms and file uploads to other web applications.


Version published
Weekly downloads
72M
increased by1.62%
Maintainers
5
Install size
517 kB
Created
Weekly downloads
 

Package description

What is form-data?

The form-data npm package is used to create `multipart/form-data` streams that can be used to perform HTTP requests with file uploads and other form data. It is commonly used with request libraries to submit forms and upload files to a server.

What are form-data's main functionalities?

Appending fields

This feature allows you to append key-value pairs to the form-data object, which can represent text fields in a form.

const FormData = require('form-data');
const form = new FormData();
form.append('username', 'exampleUser');
form.append('password', 'examplePassword');

Appending files

This feature allows you to append files to the form-data object, which can be used to upload files to a server.

const FormData = require('form-data');
const fs = require('fs');
const form = new FormData();
form.append('file', fs.createReadStream('/path/to/file.txt'), 'file.txt');

Custom headers

This feature allows you to retrieve custom headers required for submitting the form-data object, including the correct `Content-Type` header with the boundary.

const FormData = require('form-data');
const form = new FormData();
const customHeaders = form.getHeaders({'custom-header': 'value'});
// customHeaders can now be used with an HTTP client to send the form with custom headers.

Piping to a HTTP request

This feature demonstrates how to pipe the form-data directly to an HTTP request, which is useful for uploading files and submitting forms.

const FormData = require('form-data');
const http = require('http');
const form = new FormData();
form.append('field', 'value');

const request = http.request({
  method: 'post',
  host: 'example.com',
  path: '/upload',
  headers: form.getHeaders()
});

form.pipe(request);

Other packages similar to form-data

Readme

Source

Form-Data Build Status

A module to create readable "multipart/form-data" streams. Can be used to submit forms and file uploads to other web applications.

The API of this module is inspired by the XMLHttpRequest-2 FormData Interface.

Install

npm install form-data

Usage

In this example we are constructing a form with 3 fields that contain a string, a buffer and a file stream.

var FormData = require('form-data');
var fs = require('fs');

var form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));

Also you can use http-response stream:

var FormData = require('form-data');
var http = require('http');

var form = new FormData();

http.request('http://nodejs.org/images/logo.png', function(response) {
  form.append('my_field', 'my value');
  form.append('my_buffer', new Buffer(10));
  form.append('my_logo', response);
});

Or @mikeal's request stream:

var FormData = require('form-data');
var request = require('request');

var form = new FormData();

form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_logo', request('http://nodejs.org/images/logo.png'));

In order to submit this form to a web application, you can use node's http client interface:

var http = require('http');

var request = http.request({
  method: 'post',
  host: 'example.org',
  path: '/upload',
  headers: form.getHeaders()
});

form.pipe(request);

request.on('response', function(res) {
  console.log(res.statusCode);
});

Or if you would prefer the 'Content-Length' header to be set for you:

form.submit('example.org/upload', function(err, res) {
  console.log(res.statusCode);
});

To use custom headers and pre-known length in parts:

var CRLF = '\r\n';
var form = new FormData();

var options = {
  header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
  knownLength: 1
};

form.append('my_buffer', buffer, options);

form.submit('http://example.com/', function(err, res) {
  if (err) throw err;
  console.log('Done');
});

Form-Data can recognize and fetch all the required information from common types of streams (fs.readStream, http.response and mikeal's request), for some other types of streams you'd need to provide "file"-related information manually:

someModule.stream(function(err, stdout, stderr) {
  if (err) throw err;

  var form = new FormData();

  form.append('file', stdout, {
    filename: 'unicycle.jpg',
    contentType: 'image/jpg',
    knownLength: 19806
  });

  form.submit('http://example.com/', function(err, res) {
    if (err) throw err;
    console.log('Done');
  });
});

For edge cases, like POST request to URL with query string or to pass HTTP auth credentials, object can be passed to form.submit() as first parameter:

form.submit({
  host: 'example.com',
  path: '/probably.php?extra=params',
  auth: 'username:password'
}, function(err, res) {
  console.log(res.statusCode);
});

TODO

  • Add new streams (0.10) support and try really hard not to break it for 0.8.x.

License

Form-Data is licensed under the MIT license.

FAQs

Last updated on 21 Apr 2013

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc