[TIMOB-18846] Parity: HTTPClient sends array of objects differently on iOS then it does on Android
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | None |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | iOS |
Labels | n/a |
Reporter | Fokke Zandbergen |
Assignee | Unknown |
Created | 2015-04-23T08:41:55.000+0000 |
Updated | 2018-02-28T19:55:54.000+0000 |
Description
A [Q&A user](https://community.appcelerator.com/topic/178/http-request-problem-with-array-data/3) discovered that when following the [docs](http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Network.HTTPClient-method-send) to send arrays, but if those arrays include objects, the result on iOS is different then it is on Android. The expected result is seen on Android, while the result on iOS is unusable because of added
+
characters, newlines and ;
.
To reproduce
Run the following code on both iOS and Android with an updated requestb.in URL.
var xhr = Titanium.Network.createHTTPClient();
xhr.open('POST', 'http://requestb.in/101jps01');
var data = {
string: 'a',
arrayOfStrings: ['a', 'b'],
arrayOfObjects: [{
'a': 'x'
}, {
'b': 'y',
}]
};
//Dealing with arrays on data
for (var key in data) {
if (Array.isArray(data[key])) {
for (var i = 0, l = data[key].length; i < l; i++) {
data[key + '[' + i + ']'] = data[key][i];
}
delete data[key];
}
}
console.log(data);
xhr.send(data);
Android
Theconsole.log
will show:
{
"string": "a",
"arrayOfStrings[0]": "a",
"arrayOfStrings[1]": "b",
"arrayOfObjects[0]": {
"a": "x"
},
"arrayOfObjects[1]": {
"b": "y"
}
}
The body shown on requestb.in will be:
arrayOfObjects%5B0%5D=%7Bc%3Dd%2C+a%3Db%7D&arrayOfStrings%5B0%5D=a&arrayOfStrings%5B1%5D=b&string=a
which decoded is the expected:
arrayOfObjects[1]={b=y}&arrayOfObjects[0]={a=x}&arrayOfStrings[1]=b&arrayOfStrings[0]=a&string=a
iOS
Theconsole.log
will show similar:
{
"arrayOfObjects[0]" = {
a = x;
};
"arrayOfObjects[1]" = {
b = y;
};
"arrayOfStrings[0]" = a;
"arrayOfStrings[1]" = b;
string = a;
}
But the body shown on requestb.in will be:
arrayOfStrings%5B1%5D=b&arrayOfObjects%5B0%5D=%7B%0A++++a+%3D+x%3B%0A%7D&arrayOfStrings%5B0%5D=a&arayOfObjects%5B1%5D=%7B%0A++++b+%3D+y%3B%0A%7D&string=a
which decoded is:
arrayOfStrings[1]=b&arrayOfObjects[0]={
++++a+=+x;
}&arrayOfStrings[0]=a&arrayOfObjects[1]={
++++b+=+y;
}&string=a
Solution
The Android output is better, although{b=y}
is no valid JSON or form data either. It might better be {"b":"y"}
?
No comments