Skip to content

Commit

Permalink
- Added: public methods with information and examples to the readme f…
Browse files Browse the repository at this point in the history
…ile.
  • Loading branch information
JBtje committed Jun 24, 2019
1 parent 0fb2f57 commit 21323f3
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Readme.md
Expand Up @@ -184,6 +184,92 @@ form.submit({
});
```

### Methods

#####_void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )
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.
```javascript
var form = new FormData();
form.append( 'my_string', 'my value' );
form.append( 'my_integer', 1 );
form.append( 'my_boolean', true );
form.append( 'my_buffer', new Buffer(10) );
form.append( 'my_array_as_json', JSON.stringify( ['bird','cute'] ) )
```

You may provide a string for options, or an object.
```javascript
// Set filename by providing a string for options
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' );

// provide an object.
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} );
```

#####_Array_ getHeaders( [**Array** _userHeaders_] )
This method ads the correct `content-type` header to the provided array of `userHeaders`.

#####_String_ getBoundary()
Return the boundary of the formData. A boundary consists of 26x `-` followed by 24 numbers
for example:
```javascript
--------------------------515890814546601021194782
```
_Note: The boundary must be unique and may not appear in the data._

#####_Buffer_ getBuffer()
Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.

```javascript
var form = new FormData();
form.append( 'my_buffer', Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) );
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg') );

axios.post( 'https://example.com/path/to/api',
form.getBuffer(),
form.getHeaders()
)
```

#####_Integer_ getLengthSync()
Same as `getLength` but synchronous.

_Note: getLengthSync __doesn't__ calculate streams length._

#####_Integer_ getLength( function _callback_ )
Returns the `Content-Length` async. The callback is used to handle errors and continue once the length has been calculated
```javascript
this.getLength(function(err, length) {
if (err) {
this._error(err);
return;
}

// add content length
request.setHeader('Content-Length', length);

...
}.bind(this));
```

#####_boolean_ hasKnownLength()
Checks if the length of added values is known.

#####_request_ submit( _params_, function _callback_ )
Submit the form to a web application.
```javascript
var form = new FormData();
form.append( 'my_string', 'Hello World' );

form.submit( 'http://example.com/', function(err, res) {
// res – response object (http.IncomingMessage) //
res.resume();
} );
```

#####_String_ toString()
Returns the form data as a string. Don't use this if you are sending files or buffers, use `getBuffer()` instead.

### Integration with other libraries

#### Request
Expand Down

0 comments on commit 21323f3

Please sign in to comment.