Skip to content

Commit

Permalink
Fix FormData to properly handle appended arrays. (facebook#32815)
Browse files Browse the repository at this point in the history
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: facebook#32815

Test Plan: Added test case

Reviewed By: lunaleaps

Differential Revision: D33369594

Pulled By: charlesbdudley

fbshipit-source-id: 0b5219a2c9f73cf16665dc417cceb4481428ad4e
  • Loading branch information
bang9 authored and Saadnajmi committed Jan 14, 2023
1 parent 51c9458 commit 5c644b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Libraries/Network/FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FormData {
// an object with a `uri` attribute. Optionally, it can also
// have a `name` and `type` attribute to specify filename and
// content type (cf. web Blob interface.)
if (typeof value === 'object' && value) {
if (typeof value === 'object' && !Array.isArray(value) && value) {
if (typeof value.name === 'string') {
headers['content-disposition'] += '; filename="' + value.name + '"';
}
Expand Down
22 changes: 22 additions & 0 deletions Libraries/Network/__tests__/FormData-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ describe('FormData', function () {
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});

it('should return non blob array', function () {
formData.append('array', [
true,
false,
undefined,
null,
{},
[],
'string',
0,
]);

const expectedPart = {
string: 'true,false,,,[object Object],,string,0',
headers: {
'content-disposition': 'form-data; name="array"',
},
fieldName: 'array',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
});

0 comments on commit 5c644b3

Please sign in to comment.