Jesse Ruder 56fef9b622 Allow duplicate keys in FormData
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 https://github.com/facebook/react-native/pull/3556

Reviewed By: svcscm

Differential Revision: D2566999

Pulled By: nicklockwood

fb-gh-sync-id: 580e52e69376ebe9693e39a386cc540802b6d94f
2015-10-21 15:18:40 -07:00

88 lines
2.5 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule FormData
* @flow
*/
'use strict';
type FormDataValue = any;
type FormDataNameValuePair = [string, FormDataValue];
type Headers = {[name: string]: string};
type FormDataPart = {
string: string;
headers: Headers;
} | {
uri: string;
headers: Headers;
name?: string;
type?: string;
};
/**
* Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests
* with mixed data (string, native files) to be submitted via XMLHttpRequest.
*
* Example:
*
* var photo = {
* uri: uriFromCameraRoll,
* type: 'image/jpeg',
* name: 'photo.jpg',
* };
*
* var body = new FormData();
* body.append('authToken', 'secret');
* body.append('photo', photo);
* body.append('title', 'A beautiful photo!');
*
* xhr.open('POST', serverURL);
* xhr.send(body);
*/
class FormData {
_parts: Array<FormDataNameValuePair>;
constructor() {
this._parts = [];
}
append(key: string, value: FormDataValue) {
// The XMLHttpRequest spec doesn't specify if duplicate keys are allowed.
// MDN says that any new values should be appended to existing values.
// In any case, major browsers allow duplicate keys, so that's what we'll do
// too. They'll simply get appended as additional form data parts in the
// request body, leaving the server to deal with them.
this._parts.push([key, value]);
}
getParts(): Array<FormDataPart> {
return this._parts.map(([name, value]) => {
var contentDisposition = 'form-data; name="' + name + '"';
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;
}
return {...value, headers, fieldName: name};
});
}
}
module.exports = FormData;