Skip to content

Commit a149f09

Browse files
authored
Merge pull request node-fetch#5 from johnsto/feature/formdata
Add support for `Body.formData`
1 parent cf68bed commit a149f09

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959
"whatwg-url": "^5.0.0"
6060
},
6161
"dependencies": {
62-
"web-streams-polyfill": "^2.1.1"
62+
"web-streams-polyfill": "^2.1.1",
63+
"busboy": "^0.3.0",
64+
"formdata-node": "^1.5.1"
6365
},
6466
"peerDependencies": {
6567
"form-data": ">= 2.1.0"

src/body.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { ReadableStream, TransformStream} from "web-streams-polyfill";
1010
import Blob, { BUFFER } from './blob.js';
1111
import FetchError from './fetch-error.js';
1212
import Stream, { PassThrough } from "stream";
13+
import Busboy from "busboy";
14+
import FormData from "formdata-node";
1315

1416
let convert;
1517
try { convert = require('encoding').convert; } catch(e) {}
@@ -281,6 +283,24 @@ Body.prototype = {
281283
return consumeBody.call(this).then(buffer => convertBody(buffer, this.headers));
282284
},
283285

286+
formData() {
287+
return consumeBody.call(this).then(buffer => {
288+
return new Promise((resolve, reject) => {
289+
var formdata = new FormData();
290+
var busboy = new Busboy({headers: {
291+
'content-type': this.headers.get('content-type'),
292+
}});
293+
busboy.on('field', (fieldname, val) => formdata.append(fieldname, val));
294+
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
295+
let val = "";
296+
file.on('data', (data) => val += data);
297+
file.on('end', () => formdata.append(fieldname, val, filename));
298+
});
299+
busboy.on('finish', () => resolve(formdata));
300+
writeToStream(busboy, this);
301+
});
302+
});
303+
},
284304

285305
};
286306

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,7 @@ describe("Request", function() {
21412141
"arrayBuffer",
21422142
"blob",
21432143
"json",
2144+
"formData",
21442145
"text",
21452146
"method",
21462147
"url",
@@ -2295,6 +2296,22 @@ describe("Request", function() {
22952296
});
22962297
});
22972298

2299+
it("should support formData() method", function() {
2300+
const url = base;
2301+
const req = new Request(url, {
2302+
method: "POST",
2303+
body: 'a=1&b=2',
2304+
headers: {
2305+
"Content-Type": "application/x-www-form-urlencoded"
2306+
}
2307+
});
2308+
expect(req.url).to.equal(url);
2309+
return req.formData().then(result => {
2310+
expect(result.get('a')).to.equal('1');
2311+
expect(result.get('b')).to.equal('2');
2312+
});
2313+
});
2314+
22982315
it("should support buffer() method", function() {
22992316
const url = base;
23002317
const req = new Request(url, {

0 commit comments

Comments
 (0)