A library to create readable
"multipart/form-data" streams. Can be used to submit forms and file uploads to other web applications.
The API of this library is inspired by the XMLHttpRequest-2 FormData Interface.
npm install --save form-data
In this example we are constructing a form with 3 fields that contain a string, a buffer and a file stream.
var FormData = ;var fs = ;var form = ;form;form;form;
Also you can use http-response stream:
var FormData = ;var http = ;var form = ;http;
Or @mikeal's request stream:
var FormData = ;var request = ;var form = ;form;form;form;
In order to submit this form to a web application, call
submit(url, [callback]) method:
For more advanced request manipulations
submit() method returns
http.ClientRequest object, or you can choose from one of the alternative submission methods.
You can provide custom options, such as
var FormData = ;var form = maxDataSize: 20971520 ;form;form;
List of available options could be found in combined-stream
Alternative submission methods
You can use node's http client interface:
var http = ;var request = http;form;request;
Or if you would prefer the
'Content-Length' header to be set for you:
To use custom headers and pre-known length in parts:
var CRLF = '\r\n';var form = ;var options =header: CRLF + '--' + form + CRLF + 'X-Custom-Header: 123' + CRLF + CRLFknownLength: 1;form;form;
Form-Data can recognize and fetch all the required information from common types of streams (
mikeal's request), for some other types of streams you'd need to provide "file"-related information manually:
filepath property overrides
filename and may contain a relative path. This is typically used when uploading multiple files from a directory.
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:
In case you need to also send custom HTTP headers with the POST request, you can use the
headers key in first parameter of
- Void append( String field, Mixed value [, Mixed options] ).
- Array getHeaders( [Array userHeaders] )
- String getBoundary()
- Buffer getBuffer()
- Integer getLengthSync()
- Integer getLength( function callback )
- Boolean hasKnownLength()
- Request submit( params, function callback )
- String toString()
Void append( String field, Mixed value [, Mixed options] )
Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user.
var form = ;form;form;form;form;form
You may provide a string for options, or an object.
// Set filename by providing a string for optionsform;// provide an object.form;
Array getHeaders( [Array userHeaders] )
This method ads the correct
content-type header to the provided array of
Return the boundary of the formData. A boundary consists of 26
- followed by 24 numbers
Note: The boundary must be unique and may not appear in the data.
Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.
var form = ;form;form;axios
getLength but synchronous.
Note: getLengthSync doesn't calculate streams length.
Integer getLength( function callback )
Content-Length async. The callback is used to handle errors and continue once the length has been calculated
Checks if the length of added values is known.
Request submit( params, function callback )
Submit the form to a web application.
var form = ;form;form;
Returns the form data as a string. Don't use this if you are sending files or buffers, use
Integration with other libraries
Form submission using request:
var formData =my_field: 'my_value'my_file: fs;request;
For more details see request readme.
You can also submit a form using node-fetch:
var form = ;form;;
In Node.js you can post a file using axios:
const form = ;const stream = fs;form;// In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders`const formHeaders = form;axios
getLengthSync()method DOESN'T calculate length for streams, use
knownLengthoptions as workaround.
- Starting version
2.xFormData has dropped support for
Form-Data is released under the MIT license.