@@ -42,7 +42,7 @@ var http = require('http');
4242
4343var form = new FormData ();
4444
45- http .request (' http://nodejs.org/images/logo.png' , function (response ) {
45+ http .request (' http://nodejs.org/images/logo.png' , function (response ) {
4646 form .append (' my_field' , ' my value' );
4747 form .append (' my_buffer' , new Buffer (10 ));
4848 form .append (' my_logo' , response);
@@ -65,7 +65,7 @@ form.append('my_logo', request('http://nodejs.org/images/logo.png'));
6565In order to submit this form to a web application, call ``` submit(url, [callback]) ``` method:
6666
6767``` javascript
68- form .submit (' http://example.org/' , function (err , res ) {
68+ form .submit (' http://example.org/' , function (err , res ) {
6969 // res – response object (http.IncomingMessage) //
7070 res .resume ();
7171});
@@ -104,15 +104,15 @@ var request = http.request({
104104
105105form .pipe (request);
106106
107- request .on (' response' , function (res ) {
107+ request .on (' response' , function (res ) {
108108 console .log (res .statusCode );
109109});
110110```
111111
112112Or if you would prefer the ` 'Content-Length' ` header to be set for you:
113113
114114``` javascript
115- form .submit (' example.org/upload' , function (err , res ) {
115+ form .submit (' example.org/upload' , function (err , res ) {
116116 console .log (res .statusCode );
117117});
118118```
@@ -130,7 +130,7 @@ var options = {
130130
131131form .append (' my_buffer' , buffer, options);
132132
133- form .submit (' http://example.com/' , function (err , res ) {
133+ form .submit (' http://example.com/' , function (err , res ) {
134134 if (err) throw err;
135135 console .log (' Done' );
136136});
@@ -139,7 +139,7 @@ form.submit('http://example.com/', function(err, res) {
139139Form-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:
140140
141141``` javascript
142- someModule .stream (function (err , stdout , stderr ) {
142+ someModule .stream (function (err , stdout , stderr ) {
143143 if (err) throw err;
144144
145145 var form = new FormData ();
@@ -151,7 +151,7 @@ someModule.stream(function(err, stdout, stderr) {
151151 knownLength: 19806
152152 });
153153
154- form .submit (' http://example.com/' , function (err , res ) {
154+ form .submit (' http://example.com/' , function (err , res ) {
155155 if (err) throw err;
156156 console .log (' Done' );
157157 });
@@ -167,7 +167,7 @@ form.submit({
167167 host: ' example.com' ,
168168 path: ' /probably.php?extra=params' ,
169169 auth: ' username:password'
170- }, function (err , res ) {
170+ }, function (err , res ) {
171171 console .log (res .statusCode );
172172});
173173```
@@ -178,8 +178,8 @@ In case you need to also send custom HTTP headers with the POST request, you can
178178form .submit ({
179179 host: ' example.com' ,
180180 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 ) {
183183 console .log (res .statusCode );
184184});
185185```
@@ -201,20 +201,20 @@ form.submit({
201201Append 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.
202202``` javascript
203203var 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' ]));
209209```
210210
211211You may provide a string for options, or an object.
212212``` javascript
213213// 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' );
215215
216216// 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 } );
218218```
219219
220220#### _ Headers_ getHeaders( [ ** Headers** _ userHeaders_ ] )
@@ -236,13 +236,10 @@ _Note: The boundary must be unique and may not appear in the data._
236236Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.
237237``` javascript
238238var 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' ));
241241
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 ());
246243```
247244** 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.
248245
@@ -251,10 +248,10 @@ Same as `getLength` but synchronous.
251248
252249_ Note: getLengthSync __ doesn't__ calculate streams length._
253250
254- #### _ Integer_ getLength( ** function** _ callback_ )
251+ #### _ Integer_ getLength(** function** _ callback_ )
255252Returns the ` Content-Length ` async. The callback is used to handle errors and continue once the length has been calculated
256253``` javascript
257- this .getLength (function (err , length ) {
254+ this .getLength (function (err , length ) {
258255 if (err) {
259256 this ._error (err);
260257 return ;
@@ -270,13 +267,13 @@ this.getLength(function(err, length) {
270267#### _ Boolean_ hasKnownLength()
271268Checks if the length of added values is known.
272269
273- #### _ Request_ submit( _ params_ , ** function** _ callback_ )
270+ #### _ Request_ submit(_ params_ , ** function** _ callback_ )
274271Submit the form to a web application.
275272``` javascript
276273var form = new FormData ();
277- form .append ( ' my_string' , ' Hello World' );
274+ form .append (' my_string' , ' Hello World' );
278275
279- form .submit ( ' http://example.com/' , function (err , res ) {
276+ form .submit (' http://example.com/' , function (err , res ) {
280277 // res – response object (http.IncomingMessage) //
281278 res .resume ();
282279} );
@@ -297,7 +294,7 @@ var formData = {
297294 my_file: fs .createReadStream (__dirname + ' /unicycle.jpg' ),
298295};
299296
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 ) {
301298 if (err) {
302299 return console .error (' upload failed:' , err);
303300 }
@@ -317,9 +314,9 @@ var form = new FormData();
317314form .append (' a' , 1 );
318315
319316fetch (' http://example.com' , { method: ' POST' , body: form })
320- .then (function (res ) {
317+ .then (function (res ) {
321318 return res .json ();
322- }).then (function (json ) {
319+ }).then (function (json ) {
323320 console .log (json);
324321 });
325322```
@@ -341,8 +338,8 @@ axios.post('http://example.com', form, {
341338 ... formHeaders,
342339 },
343340})
344- .then (response => response)
345- .catch (error => error)
341+ .then (response => response)
342+ .catch (error => error)
346343```
347344
348345## Notes
0 commit comments