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
9 [![Linux Build](https://img.shields.io/travis/form-data/form-data/v2.1.2.svg?label=linux:0.12-6.x)](https://travis-ci.org/form-data/form-data)
10 [![MacOS Build](https://img.shields.io/travis/form-data/form-data/v2.1.2.svg?label=macos:0.12-6.x)](https://travis-ci.org/form-data/form-data)
11 [![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/v2.1.2.svg?label=windows:0.12-6.x)](https://ci.appveyor.com/project/alexindigo/form-data)
13 [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/v2.1.2.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 [![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data)
20 npm install --save form-data
25 In this example we are constructing a form with 3 fields that contain a string,
26 a buffer and a file stream.
29 var FormData = require('form-data');
30 var fs = require('fs');
32 var form = new FormData();
33 form.append('my_field', 'my value');
34 form.append('my_buffer', new Buffer(10));
35 form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
38 Also you can use http-response stream:
41 var FormData = require('form-data');
42 var http = require('http');
44 var form = new FormData();
46 http.request('http://nodejs.org/images/logo.png', function(response) {
47 form.append('my_field', 'my value');
48 form.append('my_buffer', new Buffer(10));
49 form.append('my_logo', response);
53 Or @mikeal's [request](https://github.com/request/request) stream:
56 var FormData = require('form-data');
57 var request = require('request');
59 var form = new FormData();
61 form.append('my_field', 'my value');
62 form.append('my_buffer', new Buffer(10));
63 form.append('my_logo', request('http://nodejs.org/images/logo.png'));
66 In order to submit this form to a web application, call ```submit(url, [callback])``` method:
69 form.submit('http://example.org/', function(err, res) {
70 // res – response object (http.IncomingMessage) //
76 For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods.
78 ### Alternative submission methods
80 You can use node's http client interface:
83 var http = require('http');
85 var request = http.request({
89 headers: form.getHeaders()
94 request.on('response', function(res) {
95 console.log(res.statusCode);
99 Or if you would prefer the `'Content-Length'` header to be set for you:
102 form.submit('example.org/upload', function(err, res) {
103 console.log(res.statusCode);
107 To use custom headers and pre-known length in parts:
111 var form = new FormData();
114 header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
118 form.append('my_buffer', buffer, options);
120 form.submit('http://example.com/', function(err, res) {
126 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:
129 someModule.stream(function(err, stdout, stderr) {
132 var form = new FormData();
134 form.append('file', stdout, {
135 filename: 'unicycle.jpg',
136 contentType: 'image/jpg',
140 form.submit('http://example.com/', function(err, res) {
147 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:
152 path: '/probably.php?extra=params',
153 auth: 'username:password'
154 }, function(err, res) {
155 console.log(res.statusCode);
159 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()`:
164 path: '/surelynot.php',
165 headers: {'x-test-header': 'test-header-value'}
166 }, function(err, res) {
167 console.log(res.statusCode);
171 ### Integration with other libraries
175 Form submission using [request](https://github.com/request/request):
179 my_field: 'my_value',
180 my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
183 request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) {
185 return console.error('upload failed:', err);
187 console.log('Upload successful! Server responded with:', body);
191 For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads).
195 You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch):
198 var form = new FormData();
202 fetch('http://example.com', { method: 'POST', body: form })
203 .then(function(res) {
205 }).then(function(json) {
212 - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
213 - Starting version `2.x` FormData has dropped support for `node@0.10.x`.
217 Form-Data is released under the [MIT](License) license.