Skip to content

Commit

Permalink
FormData can append only string or object with uri
Browse files Browse the repository at this point in the history
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
  • Loading branch information
doochik authored and facebook-github-bot-4 committed Jan 12, 2016
1 parent 43dcdaf commit 5061fde
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Libraries/Network/FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,22 @@ class FormData {

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

// The body part is a "blob", which in React Native just means
// 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.name === 'string') {
headers['content-disposition'] += '; filename="' + value.name + '"';
}
if (typeof value.type === 'string') {
headers['content-type'] = value.type;
if (typeof value === 'object') {
if (typeof value.name === 'string') {
headers['content-disposition'] += '; filename="' + value.name + '"';
}
if (typeof value.type === 'string') {
headers['content-type'] = value.type;
}
return {...value, headers, fieldName: name};
}
return {...value, headers, fieldName: name};
// Cast to string all other values
return {string: String(value), headers, fieldName: name};
});
}
}
Expand Down

0 comments on commit 5061fde

Please sign in to comment.