@@ -6,13 +6,12 @@ The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface]
66
77[ xhr2-fd ] : http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface
88
9- [ ![ Linux Build] ( https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4 .x-9 .x )] ( https://travis-ci.org/form-data/form-data )
10- [ ![ MacOS Build] ( https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4 .x-9 .x )] ( https://travis-ci.org/form-data/form-data )
11- [ ![ Windows Build] ( https://img.shields.io/appveyor/ci/alexindigo/ form-data/master.svg?label=windows:4 .x-9 .x )] ( https://ci.appveyor.com/project/alexindigo /form-data )
9+ [ ![ Linux Build] ( https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:6 .x-12 .x )] ( https://travis-ci.org/form-data/form-data )
10+ [ ![ MacOS Build] ( https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:6 .x-12 .x )] ( https://travis-ci.org/form-data/form-data )
11+ [ ![ Windows Build] ( https://img.shields.io/travis/form-data/ form-data/master.svg?label=windows:6 .x-12 .x )] ( https://travis- ci.org/form-data /form-data )
1212
1313[ ![ Coverage Status] ( https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage )] ( https://coveralls.io/github/form-data/form-data?branch=master )
1414[ ![ 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 )
1615
1716## Install
1817
@@ -185,6 +184,107 @@ form.submit({
185184});
186185```
187186
187+ ### Methods
188+
189+ - [ _ Void_ append( ** String** _ field_ , ** Mixed** _ value_ [ , ** Mixed** _ options_ ] )] ( https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options- ) .
190+ - [ _ Headers_ getHeaders( [ ** Headers** _ userHeaders_ ] )] ( https://github.com/form-data/form-data#array-getheaders-array-userheaders- )
191+ - [ _ String_ getBoundary()] ( https://github.com/form-data/form-data#string-getboundary )
192+ - [ _ Void_ setBoundary()] ( https://github.com/form-data/form-data#void-setboundary )
193+ - [ _ Buffer_ getBuffer()] ( https://github.com/form-data/form-data#buffer-getbuffer )
194+ - [ _ Integer_ getLengthSync()] ( https://github.com/form-data/form-data#integer-getlengthsync )
195+ - [ _ Integer_ getLength( ** function** _ callback_ )] ( https://github.com/form-data/form-data#integer-getlength-function-callback- )
196+ - [ _ Boolean_ hasKnownLength()] ( https://github.com/form-data/form-data#boolean-hasknownlength )
197+ - [ _ Request_ submit( _ params_ , ** function** _ callback_ )] ( https://github.com/form-data/form-data#request-submit-params-function-callback- )
198+ - [ _ String_ toString()] ( https://github.com/form-data/form-data#string-tostring )
199+
200+ #### _ Void_ append( ** String** _ field_ , ** Mixed** _ value_ [ , ** Mixed** _ options_ ] )
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+ ``` javascript
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' ] ) )
209+ ```
210+
211+ You may provide a string for options, or an object.
212+ ``` javascript
213+ // Set filename by providing a string for options
214+ form .append ( ' my_file' , fs .createReadStream (' /foo/bar.jpg' ), ' bar.jpg' );
215+
216+ // provide an object.
217+ form .append ( ' my_file' , fs .createReadStream (' /foo/bar.jpg' ), {filename: ' bar.jpg' , contentType: ' image/jpeg' , knownLength: 19806 } );
218+ ```
219+
220+ #### _ Headers_ getHeaders( [ ** Headers** _ userHeaders_ ] )
221+ This method adds the correct ` content-type ` header to the provided array of ` userHeaders ` .
222+
223+ #### _ String_ getBoundary()
224+ Return the boundary of the formData. By default, the boundary consists of 26 ` - ` followed by 24 numbers
225+ for example:
226+ ``` javascript
227+ -------------------------- 515890814546601021194782
228+ ```
229+
230+ #### _ Void_ setBoundary(String _ boundary_ )
231+ Set the boundary string, overriding the default behavior described above.
232+
233+ _ Note: The boundary must be unique and may not appear in the data._
234+
235+ #### _ Buffer_ getBuffer()
236+ Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.
237+ ``` javascript
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' ) );
241+
242+ axios .post ( ' https://example.com/path/to/api' ,
243+ form .getBuffer (),
244+ form .getHeaders ()
245+ )
246+ ```
247+ ** 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+
249+ #### _ Integer_ getLengthSync()
250+ Same as ` getLength ` but synchronous.
251+
252+ _ Note: getLengthSync __ doesn't__ calculate streams length._
253+
254+ #### _ Integer_ getLength( ** function** _ callback_ )
255+ Returns the ` Content-Length ` async. The callback is used to handle errors and continue once the length has been calculated
256+ ``` javascript
257+ this .getLength (function (err , length ) {
258+ if (err) {
259+ this ._error (err);
260+ return ;
261+ }
262+
263+ // add content length
264+ request .setHeader (' Content-Length' , length);
265+
266+ ...
267+ }.bind (this ));
268+ ```
269+
270+ #### _ Boolean_ hasKnownLength()
271+ Checks if the length of added values is known.
272+
273+ #### _ Request_ submit( _ params_ , ** function** _ callback_ )
274+ Submit the form to a web application.
275+ ``` javascript
276+ var form = new FormData ();
277+ form .append ( ' my_string' , ' Hello World' );
278+
279+ form .submit ( ' http://example.com/' , function (err , res ) {
280+ // res – response object (http.IncomingMessage) //
281+ res .resume ();
282+ } );
283+ ```
284+
285+ #### _ String_ toString()
286+ Returns the form data as a string. Don't use this if you are sending files or buffers, use ` getBuffer() ` instead.
287+
188288### Integration with other libraries
189289
190290#### Request
@@ -224,12 +324,34 @@ fetch('http://example.com', { method: 'POST', body: form })
224324 });
225325```
226326
327+ #### axios
328+
329+ In Node.js you can post a file using [ axios] ( https://github.com/axios/axios ) :
330+ ``` javascript
331+ const form = new FormData ();
332+ const stream = fs .createReadStream (PATH_TO_FILE );
333+
334+ form .append (' image' , stream);
335+
336+ // In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders`
337+ const formHeaders = form .getHeaders ();
338+
339+ axios .post (' http://example.com' , form, {
340+ headers: {
341+ ... formHeaders,
342+ },
343+ })
344+ .then (response => response)
345+ .catch (error => error)
346+ ```
347+
227348## Notes
228349
229350- ``` getLengthSync() ``` method DOESN'T calculate length for streams, use ``` knownLength ``` options as workaround.
230351- ``` getLength(cb) ``` will send an error as first parameter of callback if stream length cannot be calculated (e.g. send in custom streams w/o using ``` knownLength ``` ).
231352- ``` sbumit ``` will not add ` content-length ` if form length is unknown or not calculable.
232353- Starting version
` 2.x ` FormData has dropped support for
` [email protected] ` .
354+ - Starting version
` 3.x ` FormData has dropped support for
` [email protected] ` .
233355
234356## License
235357
0 commit comments