1
# Form-Data [![Build Status](https://travis-ci.org/felixge/node-form-data.png?branch=master)](https://travis-ci.org/felixge/node-form-data) [![Dependency Status](https://gemnasium.com/felixge/node-form-data.png)](https://gemnasium.com/felixge/node-form-data)
3
A module to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications.
5
The API of this module is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd].
7
[xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface
8
[streams2-thing]: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions
18
In this example we are constructing a form with 3 fields that contain a string,
19
a buffer and a file stream.
22
var FormData = require('form-data');
23
var fs = require('fs');
25
var form = new FormData();
26
form.append('my_field', 'my value');
27
form.append('my_buffer', new Buffer(10));
28
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
31
Also you can use http-response stream:
34
var FormData = require('form-data');
35
var http = require('http');
37
var form = new FormData();
39
http.request('http://nodejs.org/images/logo.png', function(response) {
40
form.append('my_field', 'my value');
41
form.append('my_buffer', new Buffer(10));
42
form.append('my_logo', response);
46
Or @mikeal's request stream:
49
var FormData = require('form-data');
50
var request = require('request');
52
var form = new FormData();
54
form.append('my_field', 'my value');
55
form.append('my_buffer', new Buffer(10));
56
form.append('my_logo', request('http://nodejs.org/images/logo.png'));
59
In order to submit this form to a web application, call ```submit(url, [callback])``` method:
62
form.submit('http://example.org/', function(err, res) {
63
// res – response object (http.IncomingMessage) //
64
res.resume(); // for node-0.10.x
69
For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods.
71
### Alternative submission methods
73
You can use node's http client interface:
76
var http = require('http');
78
var request = http.request({
82
headers: form.getHeaders()
87
request.on('response', function(res) {
88
console.log(res.statusCode);
92
Or if you would prefer the `'Content-Length'` header to be set for you:
95
form.submit('example.org/upload', function(err, res) {
96
console.log(res.statusCode);
100
To use custom headers and pre-known length in parts:
104
var form = new FormData();
107
header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
111
form.append('my_buffer', buffer, options);
113
form.submit('http://example.com/', function(err, res) {
119
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:
122
someModule.stream(function(err, stdout, stderr) {
125
var form = new FormData();
127
form.append('file', stdout, {
128
filename: 'unicycle.jpg',
129
contentType: 'image/jpg',
133
form.submit('http://example.com/', function(err, res) {
140
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:
145
path: '/probably.php?extra=params',
146
auth: 'username:password'
147
}, function(err, res) {
148
console.log(res.statusCode);
154
- ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
155
- If it feels like FormData hangs after submit and you're on ```node-0.10```, please check [Compatibility with Older Node Versions][streams2-thing]
159
- Add new streams (0.10) support and try really hard not to break it for 0.8.x.
163
Form-Data is licensed under the MIT license.