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.
npm install 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.
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:
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
getLengthSync()method DOESN'T calculate length for streams, use
knownLengthoptions as workaround.
- If it feels like FormData hangs after submit and you're on
node-0.10, please check Compatibility with Older Node Versions
- Add new streams (0.10) support and try really hard not to break it for 0.8.x.
Form-Data is licensed under the MIT license.