Skip to content

Commit 55d90ce

Browse files
authored
feat: add setBoundary method
1 parent d702625 commit 55d90ce

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

Readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ form.submit({
189189
- [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-).
190190
- [_Headers_ getHeaders( [**Headers** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-)
191191
- [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary)
192+
- [_Void_ setBoundary()](https://github.com/form-data/form-data#void-setboundary)
192193
- [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer)
193194
- [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync)
194195
- [_Integer_ getLength( **function** _callback_ )](https://github.com/form-data/form-data#integer-getlength-function-callback-)
@@ -220,11 +221,15 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg
220221
This method adds the correct `content-type` header to the provided array of `userHeaders`.
221222

222223
#### _String_ getBoundary()
223-
Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers
224+
Return the boundary of the formData. By default, the boundary consists of 26 `-` followed by 24 numbers
224225
for example:
225226
```javascript
226227
--------------------------515890814546601021194782
227228
```
229+
230+
#### _Void_ setBoundary(String _boundary_)
231+
Set the boundary string, overriding the default behavior described above.
232+
228233
_Note: The boundary must be unique and may not appear in the data._
229234

230235
#### _Buffer_ getBuffer()

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ declare class FormData extends stream.Readable {
3636
callback?: (error: Error | null, response: http.IncomingMessage) => void
3737
): http.ClientRequest;
3838
getBuffer(): Buffer;
39+
setBoundary(boundary: string): void;
3940
getBoundary(): string;
4041
getLength(callback: (err: Error | null, length: number) => void): void;
4142
getLengthSync(): number;

lib/form_data.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ FormData.prototype.getHeaders = function(userHeaders) {
305305
return formHeaders;
306306
};
307307

308+
FormData.prototype.setBoundary = function(boundary) {
309+
this._boundary = boundary;
310+
};
311+
308312
FormData.prototype.getBoundary = function() {
309313
if (!this._boundary) {
310314
this._generateBoundary();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var common = require('../common');
2+
var assert = common.assert;
3+
4+
var FormData = require(common.dir.lib + '/form_data');
5+
6+
(function testSetBoundary() {
7+
var userBoundary = '---something';
8+
var form = new FormData();
9+
form.setBoundary(userBoundary);
10+
11+
assert.equal(form.getBoundary(), userBoundary);
12+
})();
13+
14+
(function testUniqueBoundaryPerFormAfterSet() {
15+
var userBoundary = '---something';
16+
var formA = new FormData();
17+
formA.setBoundary(userBoundary);
18+
19+
var formB = new FormData();
20+
21+
assert.equal(formA.getBoundary(), userBoundary);
22+
assert.notEqual(formA.getBoundary(), formB.getBoundary());
23+
})();

0 commit comments

Comments
 (0)