@@ -42,7 +42,7 @@ var http = require('http');
42
42
43
43
var form = new FormData ();
44
44
45
- http .request (' http://nodejs.org/images/logo.png' , function (response ) {
45
+ http .request (' http://nodejs.org/images/logo.png' , function (response ) {
46
46
form .append (' my_field' , ' my value' );
47
47
form .append (' my_buffer' , new Buffer (10 ));
48
48
form .append (' my_logo' , response);
@@ -65,7 +65,7 @@ form.append('my_logo', request('http://nodejs.org/images/logo.png'));
65
65
In order to submit this form to a web application, call ``` submit(url, [callback]) ``` method:
66
66
67
67
``` javascript
68
- form .submit (' http://example.org/' , function (err , res ) {
68
+ form .submit (' http://example.org/' , function (err , res ) {
69
69
// res – response object (http.IncomingMessage) //
70
70
res .resume ();
71
71
});
@@ -104,15 +104,15 @@ var request = http.request({
104
104
105
105
form .pipe (request);
106
106
107
- request .on (' response' , function (res ) {
107
+ request .on (' response' , function (res ) {
108
108
console .log (res .statusCode );
109
109
});
110
110
```
111
111
112
112
Or if you would prefer the ` 'Content-Length' ` header to be set for you:
113
113
114
114
``` javascript
115
- form .submit (' example.org/upload' , function (err , res ) {
115
+ form .submit (' example.org/upload' , function (err , res ) {
116
116
console .log (res .statusCode );
117
117
});
118
118
```
@@ -130,7 +130,7 @@ var options = {
130
130
131
131
form .append (' my_buffer' , buffer, options);
132
132
133
- form .submit (' http://example.com/' , function (err , res ) {
133
+ form .submit (' http://example.com/' , function (err , res ) {
134
134
if (err) throw err;
135
135
console .log (' Done' );
136
136
});
@@ -139,7 +139,7 @@ form.submit('http://example.com/', function(err, res) {
139
139
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:
140
140
141
141
``` javascript
142
- someModule .stream (function (err , stdout , stderr ) {
142
+ someModule .stream (function (err , stdout , stderr ) {
143
143
if (err) throw err;
144
144
145
145
var form = new FormData ();
@@ -151,7 +151,7 @@ someModule.stream(function(err, stdout, stderr) {
151
151
knownLength: 19806
152
152
});
153
153
154
- form .submit (' http://example.com/' , function (err , res ) {
154
+ form .submit (' http://example.com/' , function (err , res ) {
155
155
if (err) throw err;
156
156
console .log (' Done' );
157
157
});
@@ -167,7 +167,7 @@ form.submit({
167
167
host: ' example.com' ,
168
168
path: ' /probably.php?extra=params' ,
169
169
auth: ' username:password'
170
- }, function (err , res ) {
170
+ }, function (err , res ) {
171
171
console .log (res .statusCode );
172
172
});
173
173
```
@@ -178,8 +178,8 @@ In case you need to also send custom HTTP headers with the POST request, you can
178
178
form .submit ({
179
179
host: ' example.com' ,
180
180
path: ' /surelynot.php' ,
181
- headers: {' x-test-header' : ' test-header-value' }
182
- }, function (err , res ) {
181
+ headers: { ' x-test-header' : ' test-header-value' }
182
+ }, function (err , res ) {
183
183
console .log (res .statusCode );
184
184
});
185
185
```
@@ -201,20 +201,20 @@ form.submit({
201
201
Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user.
202
202
``` javascript
203
203
var form = new FormData ();
204
- form .append ( ' my_string' , ' my value' );
205
- form .append ( ' my_integer' , 1 );
206
- form .append ( ' my_boolean' , true );
207
- form .append ( ' my_buffer' , new Buffer (10 ) );
208
- form .append ( ' my_array_as_json' , JSON .stringify ( [' bird' ,' cute' ] ) )
204
+ form .append (' my_string' , ' my value' );
205
+ form .append (' my_integer' , 1 );
206
+ form .append (' my_boolean' , true );
207
+ form .append (' my_buffer' , new Buffer (10 ));
208
+ form .append (' my_array_as_json' , JSON .stringify ([' bird' , ' cute' ]));
209
209
```
210
210
211
211
You may provide a string for options, or an object.
212
212
``` javascript
213
213
// Set filename by providing a string for options
214
- form .append ( ' my_file' , fs .createReadStream (' /foo/bar.jpg' ), ' bar.jpg' );
214
+ form .append (' my_file' , fs .createReadStream (' /foo/bar.jpg' ), ' bar.jpg' );
215
215
216
216
// provide an object.
217
- form .append ( ' my_file' , fs .createReadStream (' /foo/bar.jpg' ), {filename: ' bar.jpg' , contentType: ' image/jpeg' , knownLength: 19806 } );
217
+ form .append (' my_file' , fs .createReadStream (' /foo/bar.jpg' ), { filename: ' bar.jpg' , contentType: ' image/jpeg' , knownLength: 19806 } );
218
218
```
219
219
220
220
#### _ Headers_ getHeaders( [ ** Headers** _ userHeaders_ ] )
@@ -236,13 +236,10 @@ _Note: The boundary must be unique and may not appear in the data._
236
236
Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.
237
237
``` javascript
238
238
var form = new FormData ();
239
- form .append ( ' my_buffer' , Buffer .from ([0x4a ,0x42 ,0x20 ,0x52 ,0x6f ,0x63 ,0x6b ,0x73 ]) );
240
- form .append ( ' my_file' , fs .readFileSync (' /foo/bar.jpg' ) );
239
+ form .append (' my_buffer' , Buffer .from ([0x4a ,0x42 ,0x20 ,0x52 ,0x6f ,0x63 ,0x6b ,0x73 ]));
240
+ form .append (' my_file' , fs .readFileSync (' /foo/bar.jpg' ));
241
241
242
- axios .post ( ' https://example.com/path/to/api' ,
243
- form .getBuffer (),
244
- form .getHeaders ()
245
- )
242
+ axios .post (' https://example.com/path/to/api' , form .getBuffer (), form .getHeaders ());
246
243
```
247
244
** Note:** Because the output is of type Buffer, you can only append types that are accepted by Buffer: * string, Buffer, ArrayBuffer, Array, or Array-like Object* . A ReadStream for example will result in an error.
248
245
@@ -251,10 +248,10 @@ Same as `getLength` but synchronous.
251
248
252
249
_ Note: getLengthSync __ doesn't__ calculate streams length._
253
250
254
- #### _ Integer_ getLength( ** function** _ callback_ )
251
+ #### _ Integer_ getLength(** function** _ callback_ )
255
252
Returns the ` Content-Length ` async. The callback is used to handle errors and continue once the length has been calculated
256
253
``` javascript
257
- this .getLength (function (err , length ) {
254
+ this .getLength (function (err , length ) {
258
255
if (err) {
259
256
this ._error (err);
260
257
return ;
@@ -270,13 +267,13 @@ this.getLength(function(err, length) {
270
267
#### _ Boolean_ hasKnownLength()
271
268
Checks if the length of added values is known.
272
269
273
- #### _ Request_ submit( _ params_ , ** function** _ callback_ )
270
+ #### _ Request_ submit(_ params_ , ** function** _ callback_ )
274
271
Submit the form to a web application.
275
272
``` javascript
276
273
var form = new FormData ();
277
- form .append ( ' my_string' , ' Hello World' );
274
+ form .append (' my_string' , ' Hello World' );
278
275
279
- form .submit ( ' http://example.com/' , function (err , res ) {
276
+ form .submit (' http://example.com/' , function (err , res ) {
280
277
// res – response object (http.IncomingMessage) //
281
278
res .resume ();
282
279
} );
@@ -297,7 +294,7 @@ var formData = {
297
294
my_file: fs .createReadStream (__dirname + ' /unicycle.jpg' ),
298
295
};
299
296
300
- request .post ({url: ' http://service.com/upload' , formData: formData}, function (err , httpResponse , body ) {
297
+ request .post ({url: ' http://service.com/upload' , formData: formData}, function (err , httpResponse , body ) {
301
298
if (err) {
302
299
return console .error (' upload failed:' , err);
303
300
}
@@ -317,9 +314,9 @@ var form = new FormData();
317
314
form .append (' a' , 1 );
318
315
319
316
fetch (' http://example.com' , { method: ' POST' , body: form })
320
- .then (function (res ) {
317
+ .then (function (res ) {
321
318
return res .json ();
322
- }).then (function (json ) {
319
+ }).then (function (json ) {
323
320
console .log (json);
324
321
});
325
322
```
@@ -341,8 +338,8 @@ axios.post('http://example.com', form, {
341
338
... formHeaders,
342
339
},
343
340
})
344
- .then (response => response)
345
- .catch (error => error)
341
+ .then (response => response)
342
+ .catch (error => error)
346
343
```
347
344
348
345
## Notes
0 commit comments