| 1 | # Form-Data [](https://www.npmjs.com/package/form-data) [](https://gitter.im/form-data/form-data) |
| 2 | |
| 3 | A library to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications. |
| 4 | |
| 5 | The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd]. |
| 6 | |
| 7 | [xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface |
| 8 | |
| 9 | [](https://travis-ci.org/form-data/form-data) |
| 10 | [](https://travis-ci.org/form-data/form-data) |
| 11 | [](https://ci.appveyor.com/project/alexindigo/form-data) |
| 12 | |
| 13 | [](https://coveralls.io/github/form-data/form-data?branch=master) |
| 14 | [](https://david-dm.org/form-data/form-data) |
| 15 | [](https://www.bithound.io/github/form-data/form-data) |
| 16 | |
| 17 | ## Install |
| 18 | |
| 19 | ``` |
| 20 | npm install --save form-data |
| 21 | ``` |
| 22 | |
| 23 | ## Usage |
| 24 | |
| 25 | In this example we are constructing a form with 3 fields that contain a string, |
| 26 | a buffer and a file stream. |
| 27 | |
| 28 | ``` javascript |
| 29 | var FormData = require('form-data'); |
| 30 | var fs = require('fs'); |
| 31 | |
| 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')); |
| 36 | ``` |
| 37 | |
| 38 | Also you can use http-response stream: |
| 39 | |
| 40 | ``` javascript |
| 41 | var FormData = require('form-data'); |
| 42 | var http = require('http'); |
| 43 | |
| 44 | var form = new FormData(); |
| 45 | |
| 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); |
| 50 | }); |
| 51 | ``` |
| 52 | |
| 53 | Or @mikeal's [request](https://github.com/request/request) stream: |
| 54 | |
| 55 | ``` javascript |
| 56 | var FormData = require('form-data'); |
| 57 | var request = require('request'); |
| 58 | |
| 59 | var form = new FormData(); |
| 60 | |
| 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')); |
| 64 | ``` |
| 65 | |
| 66 | In order to submit this form to a web application, call ```submit(url, [callback])``` method: |
| 67 | |
| 68 | ``` javascript |
| 69 | form.submit('http://example.org/', function(err, res) { |
| 70 | // res – response object (http.IncomingMessage) // |
| 71 | res.resume(); |
| 72 | }); |
| 73 | |
| 74 | ``` |
| 75 | |
| 76 | For more advanced request manipulations ```submit()``` method returns ```http.ClientRequest``` object, or you can choose from one of the alternative submission methods. |
| 77 | |
| 78 | ### Custom options |
| 79 | |
| 80 | You can provide custom options, such as `maxDataSize`: |
| 81 | |
| 82 | ``` javascript |
| 83 | var FormData = require('form-data'); |
| 84 | |
| 85 | var form = new FormData({ maxDataSize: 20971520 }); |
| 86 | form.append('my_field', 'my value'); |
| 87 | form.append('my_buffer', /* something big */); |
| 88 | ``` |
| 89 | |
| 90 | List of available options could be found in [combined-stream](https://github.com/felixge/node-combined-stream/blob/master/lib/combined_stream.js#L7-L15) |
| 91 | |
| 92 | ### Alternative submission methods |
| 93 | |
| 94 | You can use node's http client interface: |
| 95 | |
| 96 | ``` javascript |
| 97 | var http = require('http'); |
| 98 | |
| 99 | var request = http.request({ |
| 100 | method: 'post', |
| 101 | host: 'example.org', |
| 102 | path: '/upload', |
| 103 | headers: form.getHeaders() |
| 104 | }); |
| 105 | |
| 106 | form.pipe(request); |
| 107 | |
| 108 | request.on('response', function(res) { |
| 109 | console.log(res.statusCode); |
| 110 | }); |
| 111 | ``` |
| 112 | |
| 113 | Or if you would prefer the `'Content-Length'` header to be set for you: |
| 114 | |
| 115 | ``` javascript |
| 116 | form.submit('example.org/upload', function(err, res) { |
| 117 | console.log(res.statusCode); |
| 118 | }); |
| 119 | ``` |
| 120 | |
| 121 | To use custom headers and pre-known length in parts: |
| 122 | |
| 123 | ``` javascript |
| 124 | var CRLF = '\r\n'; |
| 125 | var form = new FormData(); |
| 126 | |
| 127 | var options = { |
| 128 | header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF, |
| 129 | knownLength: 1 |
| 130 | }; |
| 131 | |
| 132 | form.append('my_buffer', buffer, options); |
| 133 | |
| 134 | form.submit('http://example.com/', function(err, res) { |
| 135 | if (err) throw err; |
| 136 | console.log('Done'); |
| 137 | }); |
| 138 | ``` |
| 139 | |
| 140 | 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: |
| 141 | |
| 142 | ``` javascript |
| 143 | someModule.stream(function(err, stdout, stderr) { |
| 144 | if (err) throw err; |
| 145 | |
| 146 | var form = new FormData(); |
| 147 | |
| 148 | form.append('file', stdout, { |
| 149 | filename: 'unicycle.jpg', // ... or: |
| 150 | filepath: 'photos/toys/unicycle.jpg', |
| 151 | contentType: 'image/jpeg', |
| 152 | knownLength: 19806 |
| 153 | }); |
| 154 | |
| 155 | form.submit('http://example.com/', function(err, res) { |
| 156 | if (err) throw err; |
| 157 | console.log('Done'); |
| 158 | }); |
| 159 | }); |
| 160 | ``` |
| 161 | |
| 162 | The `filepath` property overrides `filename` and may contain a relative path. This is typically used when uploading [multiple files from a directory](https://wicg.github.io/entries-api/#dom-htmlinputelement-webkitdirectory). |
| 163 | |
| 164 | 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: |
| 165 | |
| 166 | ``` javascript |
| 167 | form.submit({ |
| 168 | host: 'example.com', |
| 169 | path: '/probably.php?extra=params', |
| 170 | auth: 'username:password' |
| 171 | }, function(err, res) { |
| 172 | console.log(res.statusCode); |
| 173 | }); |
| 174 | ``` |
| 175 | |
| 176 | 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()`: |
| 177 | |
| 178 | ``` javascript |
| 179 | form.submit({ |
| 180 | host: 'example.com', |
| 181 | path: '/surelynot.php', |
| 182 | headers: {'x-test-header': 'test-header-value'} |
| 183 | }, function(err, res) { |
| 184 | console.log(res.statusCode); |
| 185 | }); |
| 186 | ``` |
| 187 | |
| 188 | ### Integration with other libraries |
| 189 | |
| 190 | #### Request |
| 191 | |
| 192 | Form submission using [request](https://github.com/request/request): |
| 193 | |
| 194 | ```javascript |
| 195 | var formData = { |
| 196 | my_field: 'my_value', |
| 197 | my_file: fs.createReadStream(__dirname + '/unicycle.jpg'), |
| 198 | }; |
| 199 | |
| 200 | request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) { |
| 201 | if (err) { |
| 202 | return console.error('upload failed:', err); |
| 203 | } |
| 204 | console.log('Upload successful! Server responded with:', body); |
| 205 | }); |
| 206 | ``` |
| 207 | |
| 208 | For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads). |
| 209 | |
| 210 | #### node-fetch |
| 211 | |
| 212 | You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch): |
| 213 | |
| 214 | ```javascript |
| 215 | var form = new FormData(); |
| 216 | |
| 217 | form.append('a', 1); |
| 218 | |
| 219 | fetch('http://example.com', { method: 'POST', body: form }) |
| 220 | .then(function(res) { |
| 221 | return res.json(); |
| 222 | }).then(function(json) { |
| 223 | console.log(json); |
| 224 | }); |
| 225 | ``` |
| 226 | |
| 227 | ## Notes |
| 228 | |
| 229 | - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround. |
| 230 | - Starting version `2.x` FormData has dropped support for `node@0.10.x`. |
| 231 | |
| 232 | ## License |
| 233 | |
| 234 | Form-Data is released under the [MIT](License) license. |