diff --git a/Readme.md b/Readme.md index e9195bdb..7bfea3e3 100644 --- a/Readme.md +++ b/Readme.md @@ -189,6 +189,7 @@ form.submit({ - [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-). - [_Headers_ getHeaders( [**Headers** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-) - [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary) +- [_Void_ setBoundary()](https://github.com/form-data/form-data#void-setboundary) - [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer) - [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync) - [_Integer_ getLength( **function** _callback_ )](https://github.com/form-data/form-data#integer-getlength-function-callback-) @@ -217,14 +218,18 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg ``` #### _Headers_ getHeaders( [**Headers** _userHeaders_] ) -This method ads the correct `content-type` header to the provided array of `userHeaders`. +This method adds the correct `content-type` header to the provided array of `userHeaders`. #### _String_ getBoundary() -Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers +Return the boundary of the formData. By default, the boundary consists of 26 `-` followed by 24 numbers for example: ```javascript --------------------------515890814546601021194782 ``` + +#### _Void_ setBoundary(String _boundary_) +Set the boundary string, overriding the default behavior described above. + _Note: The boundary must be unique and may not appear in the data._ #### _Buffer_ getBuffer() diff --git a/index.d.ts b/index.d.ts index 6e520454..295e9e9b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -36,6 +36,7 @@ declare class FormData extends stream.Readable { callback?: (error: Error | null, response: http.IncomingMessage) => void ): http.ClientRequest; getBuffer(): Buffer; + setBoundary(boundary: string): void; getBoundary(): string; getLength(callback: (err: Error | null, length: number) => void): void; getLengthSync(): number; diff --git a/lib/form_data.js b/lib/form_data.js index ddfae2e3..cf836b0b 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -305,6 +305,10 @@ FormData.prototype.getHeaders = function(userHeaders) { return formHeaders; }; +FormData.prototype.setBoundary = function(boundary) { + this._boundary = boundary; +}; + FormData.prototype.getBoundary = function() { if (!this._boundary) { this._generateBoundary(); diff --git a/package.json b/package.json index 6f1ebf05..a2fcb88d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "3.0.0", + "version": "3.0.1", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" diff --git a/test/integration/test-set-boundary.js b/test/integration/test-set-boundary.js new file mode 100644 index 00000000..f1d88206 --- /dev/null +++ b/test/integration/test-set-boundary.js @@ -0,0 +1,23 @@ +var common = require('../common'); +var assert = common.assert; + +var FormData = require(common.dir.lib + '/form_data'); + +(function testSetBoundary() { + var userBoundary = '---something'; + var form = new FormData(); + form.setBoundary(userBoundary); + + assert.equal(form.getBoundary(), userBoundary); +})(); + +(function testUniqueBoundaryPerFormAfterSet() { + var userBoundary = '---something'; + var formA = new FormData(); + formA.setBoundary(userBoundary); + + var formB = new FormData(); + + assert.equal(formA.getBoundary(), userBoundary); + assert.notEqual(formA.getBoundary(), formB.getBoundary()); +})();