From 5c644b3f0dedfc67d9147beff2fdf55dbcc84113 Mon Sep 17 00:00:00 2001 From: bang9 Date: Mon, 28 Mar 2022 11:53:32 -0700 Subject: [PATCH] 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: https://github.com/facebook/react-native/pull/32815 Test Plan: Added test case Reviewed By: lunaleaps Differential Revision: D33369594 Pulled By: charlesbdudley fbshipit-source-id: 0b5219a2c9f73cf16665dc417cceb4481428ad4e --- Libraries/Network/FormData.js | 2 +- Libraries/Network/__tests__/FormData-test.js | 22 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Libraries/Network/FormData.js b/Libraries/Network/FormData.js index 8304a7d171d66b..3392ada7e93edc 100644 --- a/Libraries/Network/FormData.js +++ b/Libraries/Network/FormData.js @@ -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 + '"'; } diff --git a/Libraries/Network/__tests__/FormData-test.js b/Libraries/Network/__tests__/FormData-test.js index 4762ddf2c1f42b..e8316b30b1a8a8 100644 --- a/Libraries/Network/__tests__/FormData-test.js +++ b/Libraries/Network/__tests__/FormData-test.js @@ -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); + }); });