Skip to content

Commit 21323f3

Browse files
committed
- Added: public methods with information and examples to the readme file.
1 parent 0fb2f57 commit 21323f3

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,92 @@ form.submit({
184184
});
185185
```
186186

187+
### Methods
188+
189+
#####_void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )
190+
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.
191+
```javascript
192+
var form = new FormData();
193+
form.append( 'my_string', 'my value' );
194+
form.append( 'my_integer', 1 );
195+
form.append( 'my_boolean', true );
196+
form.append( 'my_buffer', new Buffer(10) );
197+
form.append( 'my_array_as_json', JSON.stringify( ['bird','cute'] ) )
198+
```
199+
200+
You may provide a string for options, or an object.
201+
```javascript
202+
// Set filename by providing a string for options
203+
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' );
204+
205+
// provide an object.
206+
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} );
207+
```
208+
209+
#####_Array_ getHeaders( [**Array** _userHeaders_] )
210+
This method ads the correct `content-type` header to the provided array of `userHeaders`.
211+
212+
#####_String_ getBoundary()
213+
Return the boundary of the formData. A boundary consists of 26x `-` followed by 24 numbers
214+
for example:
215+
```javascript
216+
--------------------------515890814546601021194782
217+
```
218+
_Note: The boundary must be unique and may not appear in the data._
219+
220+
#####_Buffer_ getBuffer()
221+
Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data.
222+
223+
```javascript
224+
var form = new FormData();
225+
form.append( 'my_buffer', Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) );
226+
form.append( 'my_file', fs.createReadStream('/foo/bar.jpg') );
227+
228+
axios.post( 'https://example.com/path/to/api',
229+
form.getBuffer(),
230+
form.getHeaders()
231+
)
232+
```
233+
234+
#####_Integer_ getLengthSync()
235+
Same as `getLength` but synchronous.
236+
237+
_Note: getLengthSync __doesn't__ calculate streams length._
238+
239+
#####_Integer_ getLength( function _callback_ )
240+
Returns the `Content-Length` async. The callback is used to handle errors and continue once the length has been calculated
241+
```javascript
242+
this.getLength(function(err, length) {
243+
if (err) {
244+
this._error(err);
245+
return;
246+
}
247+
248+
// add content length
249+
request.setHeader('Content-Length', length);
250+
251+
...
252+
}.bind(this));
253+
```
254+
255+
#####_boolean_ hasKnownLength()
256+
Checks if the length of added values is known.
257+
258+
#####_request_ submit( _params_, function _callback_ )
259+
Submit the form to a web application.
260+
```javascript
261+
var form = new FormData();
262+
form.append( 'my_string', 'Hello World' );
263+
264+
form.submit( 'http://example.com/', function(err, res) {
265+
// res – response object (http.IncomingMessage) //
266+
res.resume();
267+
} );
268+
```
269+
270+
#####_String_ toString()
271+
Returns the form data as a string. Don't use this if you are sending files or buffers, use `getBuffer()` instead.
272+
187273
### Integration with other libraries
188274

189275
#### Request

0 commit comments

Comments
 (0)