Skip to content

Commit d2e8e7d

Browse files
bang9facebook-github-bot
authored andcommitted
Fix FormData to properly handle appended arrays. (#32815)
Summary: The Array appended to FormData must be transmitted in the form of a string. However, it is treated as a file object and transmitted, because `typeof Array` is `'object'` too In network ```js form.append('array_name', ['a', 'b', 'c']) // Browser // Content-Disposition: form-data; name='array_name'; // a,b,c // ReactNative // Content-Disposition: form-data; name='array_name'; // ``` ## Changelog [General] [Fixed] - The Array appended to FormData is transmitted as a string Pull Request resolved: #32815 Test Plan: Added test case Reviewed By: lunaleaps Differential Revision: D33369594 Pulled By: charlesbdudley fbshipit-source-id: 0b5219a2c9f73cf16665dc417cceb4481428ad4e
1 parent 1ca2c24 commit d2e8e7d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Libraries/Network/FormData.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FormData {
7474
// an object with a `uri` attribute. Optionally, it can also
7575
// have a `name` and `type` attribute to specify filename and
7676
// content type (cf. web Blob interface.)
77-
if (typeof value === 'object' && value) {
77+
if (typeof value === 'object' && !Array.isArray(value) && value) {
7878
if (typeof value.name === 'string') {
7979
headers['content-disposition'] += '; filename="' + value.name + '"';
8080
}

Libraries/Network/__tests__/FormData-test.js

+22
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,26 @@ describe('FormData', function () {
5555
};
5656
expect(formData.getParts()[0]).toMatchObject(expectedPart);
5757
});
58+
59+
it('should return non blob array', function () {
60+
formData.append('array', [
61+
true,
62+
false,
63+
undefined,
64+
null,
65+
{},
66+
[],
67+
'string',
68+
0,
69+
]);
70+
71+
const expectedPart = {
72+
string: 'true,false,,,[object Object],,string,0',
73+
headers: {
74+
'content-disposition': 'form-data; name="array"',
75+
},
76+
fieldName: 'array',
77+
};
78+
expect(formData.getParts()[0]).toMatchObject(expectedPart);
79+
});
5880
});

0 commit comments

Comments
 (0)