Skip to content

Commit

Permalink
Allow duplicate keys in FormData
Browse files Browse the repository at this point in the history
Summary: Right now `FormData` doesn't allow duplicate keys and uses the last value set for a duplicate key. I tested this in Chrome:
```
var formData = new FormData();
formData.append('key', 'value1');
formData.append('key', 'value2');

var request = new XMLHttpRequest();
request.open("POST", serverUrl);
request.send(formData);
```
and the request has both 'value1' and 'value2'.

I removed the duplicate key check in `FormData`. If people want to build appending or disallow duplicate keys, they can build either on top of this.
Closes #3556

Reviewed By: svcscm

Differential Revision: D2566999

Pulled By: nicklockwood

fb-gh-sync-id: 580e52e69376ebe9693e39a386cc540802b6d94f
  • Loading branch information
jesseruder authored and facebook-github-bot-5 committed Oct 21, 2015
1 parent 666833d commit 56fef9b
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions Libraries/Network/FormData.js
Expand Up @@ -47,26 +47,18 @@ type FormDataPart = {
*/ */
class FormData { class FormData {
_parts: Array<FormDataNameValuePair>; _parts: Array<FormDataNameValuePair>;
_partsByKey: {[key: string]: FormDataNameValuePair};


constructor() { constructor() {
this._parts = []; this._parts = [];
this._partsByKey = {};
} }


append(key: string, value: FormDataValue) { append(key: string, value: FormDataValue) {
var parts = this._partsByKey[key]; // The XMLHttpRequest spec doesn't specify if duplicate keys are allowed.
if (parts) { // MDN says that any new values should be appended to existing values.
// It's a bit unclear what the behaviour should be in this case. // In any case, major browsers allow duplicate keys, so that's what we'll do
// The XMLHttpRequest spec doesn't specify it, while MDN says that // too. They'll simply get appended as additional form data parts in the
// the any new values should appended to existing values. We're not // request body, leaving the server to deal with them.
// doing that for now -- it's tedious and doesn't seem worth the effort. this._parts.push([key, value]);
parts[1] = value;
return;
}
parts = [key, value];
this._parts.push(parts);
this._partsByKey[key] = parts;
} }


getParts(): Array<FormDataPart> { getParts(): Array<FormDataPart> {
Expand Down

0 comments on commit 56fef9b

Please sign in to comment.