1 # Form-Data [![NPM Module](https://img.shields.io/npm/v/form-data.svg)](https://www.npmjs.com/package/form-data) [![Join the chat at https://gitter.im/form-data/form-data](http://form-data.github.io/images/gitterbadge.svg)](https://gitter.im/form-data/form-data)
3 A library 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 library 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
10 [![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:0.10-5.x)](https://travis-ci.org/form-data/form-data)
11 [![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:0.10-5.x)](https://ci.appveyor.com/project/alexindigo/form-data)
12 [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master)
14 [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)
15 [![Codacy Badge](https://img.shields.io/codacy/43ece80331c246179695e41f81eeffe2.svg)](https://www.codacy.com/app/form-data/form-data)
16 [![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data)
26 In this example we are constructing a form with 3 fields that contain a string,
27 a buffer and a file stream.
30 var FormData = require('form-data');
31 var fs = require('fs');
33 var form = new FormData();
34 form.append('my_field', 'my value');
35 form.append('my_buffer', new Buffer(10));
36 form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
39 Also you can use http-response stream:
42 var FormData = require('form-data');
43 var http = require('http');
45 var form = new FormData();
47 http.request('http://nodejs.org/images/logo.png', function(response) {
48 form.append('my_field', 'my value');
49 form.append('my_buffer', new Buffer(10));
50 form.append('my_logo', response);
54 Or @mikeal's [request](https://github.com/request/request) stream:
57 var FormData = require('form-data');
58 var request = require('request');
60 var form = new FormData();
62 form.append('my_field', 'my value');
63 form.append('my_buffer', new Buffer(10));
64 form.append('my_logo', request('http://nodejs.org/images/logo.png'));
67 In order to submit this form to a web application, call ```submit(url, [callback])``` method:
70 form.submit('http://example.org/', function(err, res) {
71 // res – response object (http.IncomingMessage) //
77 For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods.
79 ### Alternative submission methods
81 You can use node's http client interface:
84 var http = require('http');
86 var request = http.request({
90 headers: form.getHeaders()
95 request.on('response', function(res) {
96 console.log(res.statusCode);
100 Or if you would prefer the `'Content-Length'` header to be set for you:
103 form.submit('example.org/upload', function(err, res) {
104 console.log(res.statusCode);
108 To use custom headers and pre-known length in parts:
112 var form = new FormData();
115 header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
119 form.append('my_buffer', buffer, options);
121 form.submit('http://example.com/', function(err, res) {
127 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:
130 someModule.stream(function(err, stdout, stderr) {
133 var form = new FormData();
135 form.append('file', stdout, {
136 filename: 'unicycle.jpg',
137 contentType: 'image/jpg',
141 form.submit('http://example.com/', function(err, res) {
148 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:
153 path: '/probably.php?extra=params',
154 auth: 'username:password'
155 }, function(err, res) {
156 console.log(res.statusCode);
160 In case you need to also send custom HTTP headers with the POST request, you can use the `headers` key in first parameter of `form.submit()`:
165 path: '/surelynot.php',
166 headers: {'x-test-header': 'test-header-value'}
167 }, function(err, res) {
168 console.log(res.statusCode);
172 ### Integration with other libraries
176 Form submission using [request](https://github.com/request/request):
180 my_field: 'my_value',
181 my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
184 request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) {
186 return console.error('upload failed:', err);
188 console.log('Upload successful! Server responded with:', body);
192 For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads).
196 You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch):
199 var form = new FormData();
203 fetch('http://example.com', { method: 'POST', body: form })
204 .then(function(res) {
206 }).then(function(json) {
213 - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
214 - If it feels like FormData hangs after submit and you're on ```node-0.10```, please check [Compatibility with Older Node Versions][streams2-thing]
218 Form-Data is licensed under the MIT license.