Skip to content

Commit 5061fde

Browse files
doochikfacebook-github-bot-4
authored andcommitted
FormData can append only string or object with uri
Summary: I fix FormData type checking. Simple test case: ```js var fd = new FormData(); // JS Error. // Because all non-string values threaded as "blob" fd.append('number', 1); ``` Closes facebook#4390 Reviewed By: svcscm Differential Revision: D2818377 Pulled By: nicklockwood fb-gh-sync-id: 8b3f27476af21c5fd65b844034579372172ea73c
1 parent 43dcdaf commit 5061fde

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Libraries/Network/FormData.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,22 @@ class FormData {
7171

7272
/* $FlowIssue(>=0.20.1) #9463928 */
7373
var headers: Headers = {'content-disposition': contentDisposition};
74-
if (typeof value === 'string') {
75-
return {string: value, headers, fieldName: name};
76-
}
7774

7875
// The body part is a "blob", which in React Native just means
7976
// an object with a `uri` attribute. Optionally, it can also
8077
// have a `name` and `type` attribute to specify filename and
8178
// content type (cf. web Blob interface.)
82-
if (typeof value.name === 'string') {
83-
headers['content-disposition'] += '; filename="' + value.name + '"';
84-
}
85-
if (typeof value.type === 'string') {
86-
headers['content-type'] = value.type;
79+
if (typeof value === 'object') {
80+
if (typeof value.name === 'string') {
81+
headers['content-disposition'] += '; filename="' + value.name + '"';
82+
}
83+
if (typeof value.type === 'string') {
84+
headers['content-type'] = value.type;
85+
}
86+
return {...value, headers, fieldName: name};
8787
}
88-
return {...value, headers, fieldName: name};
88+
// Cast to string all other values
89+
return {string: String(value), headers, fieldName: name};
8990
});
9091
}
9192
}

0 commit comments

Comments
 (0)