[TIMOB-13357] Android: Titanium does not properly handle arrays in form-encoded POSTs
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Low |
| Status | Open |
| Resolution | Unresolved |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | Android |
| Labels | form-encoded, httpclient, post |
| Reporter | Matt Baxter |
| Assignee | Unknown |
| Created | 2012-11-12T17:36:02.000+0000 |
| Updated | 2018-02-28T20:03:38.000+0000 |
Description
When using the HTTPClient in Titanium Mobile, the documentation (http://docs.appcelerator.com/titanium/2.1/#!/api/Titanium.Network.HTTPClient-method-send) says that if you pass a serializable JavaScript object, it will automatically turn it into form-encoded POST data. However, this breaks when your data contains an array.
Using the free service httpbin (http://httpbin.org/) to view the POST data that is sent, you can see that the array is not properly encoded. By sending POST data to httpbin.org/post, we can see the POST data that Titanium sends.
Example code:
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(ev) {
Ti.API.info(this.responseText);
};
xhr.open('POST','http://httpbin.org/post');
xhr.send({
'test_text': 'testing',
'test_array': ['one','two','three']
});
The response received looks like this:
{
"files": {},
"form": {
"test_array": "[Ljava.lang.Object;@41a6e560",
"test_text": "testing"
},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "63",
"Connection": "keep-alive",
"User-Agent": "",
"Host": "httpbin.org",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded"
},
"json": null,
"data": ""
}
You will see the "test_array" has a value of "[Ljava.lang.Object;@41a6e560".
However, sending the data as a url encoded string by changing the send statement to this:
xhr.send('test_text=testing&test_array=one&test_array=two&test_array=three');
Creates the correct post data:
{
"origin": "64.128.22.8",
"files": {},
"form": {
"test_array": [
"one",
"two",
"three"
],
"test_text": "testing"
},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "64",
"Connection": "keep-alive",
"User-Agent": "",
"Host": "httpbin.org",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded"
},
"json": null,
"data": ""
}
I had the same problem. @matt Thanks for the current tip to solve this problem :) http://developer.appcelerator.com/question/152751/how-can-i-see-the-request-headers-in-http-client-along-with-the-post-message--httpclient-problem#answer-263243
Just hit this as well and finally realized it was this bug after a bunch of debugging. This seems like an important scenario (Android + POSTing and array) in an essential module. Any chance this can get some traction after a year of being an issue?
Thanks Ingo!