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)
|
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 | [![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)
|
12 |
|
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)
|
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 | ### Alternative submission methods
|
79 |
|
80 | You can use node's http client interface:
|
81 |
|
82 | ``` javascript
|
83 | var http = require('http');
|
84 |
|
85 | var request = http.request({
|
86 | method: 'post',
|
87 | host: 'example.org',
|
88 | path: '/upload',
|
89 | headers: form.getHeaders()
|
90 | });
|
91 |
|
92 | form.pipe(request);
|
93 |
|
94 | request.on('response', function(res) {
|
95 | console.log(res.statusCode);
|
96 | });
|
97 | ```
|
98 |
|
99 | Or if you would prefer the `'Content-Length'` header to be set for you:
|
100 |
|
101 | ``` javascript
|
102 | form.submit('example.org/upload', function(err, res) {
|
103 | console.log(res.statusCode);
|
104 | });
|
105 | ```
|
106 |
|
107 | To use custom headers and pre-known length in parts:
|
108 |
|
109 | ``` javascript
|
110 | var CRLF = '\r\n';
|
111 | var form = new FormData();
|
112 |
|
113 | var options = {
|
114 | header: CRLF + '--' + form.getBoundary() + CRLF + 'X-Custom-Header: 123' + CRLF + CRLF,
|
115 | knownLength: 1
|
116 | };
|
117 |
|
118 | form.append('my_buffer', buffer, options);
|
119 |
|
120 | form.submit('http://example.com/', function(err, res) {
|
121 | if (err) throw err;
|
122 | console.log('Done');
|
123 | });
|
124 | ```
|
125 |
|
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:
|
127 |
|
128 | ``` javascript
|
129 | someModule.stream(function(err, stdout, stderr) {
|
130 | if (err) throw err;
|
131 |
|
132 | var form = new FormData();
|
133 |
|
134 | form.append('file', stdout, {
|
135 | filename: 'unicycle.jpg',
|
136 | contentType: 'image/jpg',
|
137 | knownLength: 19806
|
138 | });
|
139 |
|
140 | form.submit('http://example.com/', function(err, res) {
|
141 | if (err) throw err;
|
142 | console.log('Done');
|
143 | });
|
144 | });
|
145 | ```
|
146 |
|
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:
|
148 |
|
149 | ``` javascript
|
150 | form.submit({
|
151 | host: 'example.com',
|
152 | path: '/probably.php?extra=params',
|
153 | auth: 'username:password'
|
154 | }, function(err, res) {
|
155 | console.log(res.statusCode);
|
156 | });
|
157 | ```
|
158 |
|
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()`:
|
160 |
|
161 | ``` javascript
|
162 | form.submit({
|
163 | host: 'example.com',
|
164 | path: '/surelynot.php',
|
165 | headers: {'x-test-header': 'test-header-value'}
|
166 | }, function(err, res) {
|
167 | console.log(res.statusCode);
|
168 | });
|
169 | ```
|
170 |
|
171 | ### Integration with other libraries
|
172 |
|
173 | #### Request
|
174 |
|
175 | Form submission using [request](https://github.com/request/request):
|
176 |
|
177 | ```javascript
|
178 | var formData = {
|
179 | my_field: 'my_value',
|
180 | my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
|
181 | };
|
182 |
|
183 | request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) {
|
184 | if (err) {
|
185 | return console.error('upload failed:', err);
|
186 | }
|
187 | console.log('Upload successful! Server responded with:', body);
|
188 | });
|
189 | ```
|
190 |
|
191 | For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads).
|
192 |
|
193 | #### node-fetch
|
194 |
|
195 | You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch):
|
196 |
|
197 | ```javascript
|
198 | var form = new FormData();
|
199 |
|
200 | form.append('a', 1);
|
201 |
|
202 | fetch('http://example.com', { method: 'POST', body: form })
|
203 | .then(function(res) {
|
204 | return res.json();
|
205 | }).then(function(json) {
|
206 | console.log(json);
|
207 | });
|
208 | ```
|
209 |
|
210 | ## Notes
|
211 |
|
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`.
|
214 |
|
215 | ## License
|
216 |
|
217 | Form-Data is released under the [MIT](License) license.
|
218 |
|
\ | No newline at end of file |