IOS: Fail to Set Multiple Cookie in Request Header for Ti.Network.HTTPClient in SDK 3.3.0
Reproduce and Test Case:
var sessionid = Ti.App.getSessionId();
var token = "example_token";
var contextid = '1234567';
var endpoint = "http://127.0.0.1:8888";
//Ti.API.info(endpoint);
var loader = Titanium.Network.createHTTPClient({
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
alert("Received text: " + this.responseText);
alert('success');
},
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
loader.open("GET", endpoint);
loader.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
loader.setRequestHeader('User-Agent', Titanium.userAgent);
loader.setRequestHeader('Cookie', 'Context='+contextid);
loader.setRequestHeader('Cookie', 'sessionID='+sessionid);
loader.setRequestHeader('Cookie', 'Auth_token='+token);
loader.send();
Expect Result:
Server host can get all three cookie information.
For example:
Context=1234567; sessionID=486E0EFB-C4A1-478A-9A5B-A9EB683B310C; Auth_token=example_token
Actual Result:
Only the last cookie has been passed to server.
For example:
Auth_token=example_token
It seems like the previous cookies property will be overwritten by the next when you set multiple cookies information at one request header on SDK 3.3.0.GA.
N.B. Only happens in 3.3.0.GA. It works well in 3.2.3 and 3.2.1.
Attached a simple nodejs server to get cookie information from http request.
As a workaround, can you set them as a single call?
The internal headers are a key dictionary where the first param is the key and the second the value. Setting a second cookie will replace the previous. Here's the workaround for the meantime
Sorry, Please ignore my last comment, as choosing wrong version of SDK. the workaround works well in SDK 3.3.0
This is still happening in 3.4.0.GA Also, clearCookies() does not appear to be working any more. Has the ASI Library been deprecated? Is this why these changes have occurred? Does 'APSHTTPClient' handles all the Ti.Network.HTTPClient methods now?
This should not be fixed. It is working as it should. We should update the docs with Pedro's solution
See http://stackoverflow.com/questions/1268673/set-a-request-header-in-javascript for a reference.
Advised to use workaround described by Pedro for issues like this.
No, this is not working as it should If you view the StackOverflow post, we are created a web-influenced API. Thus, we should adopt a web-like behavior, and you can set multiple cookies in the browser. Apologies if this wasn't clear before, but I do think we _should_ fix this.
PR Here: https://github.com/appcelerator/titanium_mobile/pull/6827 and PR Here: https://github.com/appcelerator/APSHTTPClient/pull/23 Mutliple calls to setRequestHeader on the 'Cookie' key, will result in the values being appended with a ';' in between each value, instead of one replacing the other.
[~pec1985], To address your github comments for the PR. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#setRequestHeader() According to this abstracted from the link, _if this method is called several times with the same header, the values are merged into one single request header._ and [~ingo]'s comment that we should follow a web-influenced API and adopt a web-like behavior, we can only use *setRequestHeader* and *not* create another method called *addRequestHeader* (although i would have preferred the latter option as well). Here is another link that shows an example for setting the same header twice: http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader()-method and the abstracted sample code here:
If it's agreed that we follow the web-like behavior, I will proceed to work on the Android portion.
After further research from earlier links, these links and the native SDK references: https://github.com/AliSoftware/OHHTTPStubs/issues/43 http://www.tutorialspoint.com/http/http_header_fields.htm http://blog.hjoseph.com/post/93171041261/setting-nsmutableurlrequest-cookies It is concluded that:
repeated calls to setRequestHeader appends the field values
if the field is cookie, append is done with ';', while other fields use ','
new sample code:
expected result (seen on from cookieTest2.js attached)
iOS APSHttpClient PR: https://github.com/appcelerator/APSHTTPClient/pull/25 iOS Master PR: https://github.com/appcelerator/titanium_mobile/pull/6844 android PR coming soon.
Android PR: https://github.com/appcelerator/titanium_mobile/pull/6845
Reopening, reason: Waiting for android PR to be merged
Closing ticket as fixed, if there are any problems, please file a new ticket.