Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Set COOKIES using setRequestHeader() = CRASHED ?

Hello,

I seem to get problems as soon as I have more than one cookie sending through setRequestHeader("Cookie","?????");

Here's my code

var cookieString = "cookie1=value1; cookie2=value2;";

xhr = Ti.Network.createHTTPClient();
xhr.open(method, url); 
xhr.setRequestHeader('Cookie', cookieString); 
xhr.send();

If I remove the second variable (cookie2=value2) from the cookieString, guess what.. it works !!

As soon as I add it back, it fails.

This seems to be the correct format for cookies in HTTP headers.
Anyone else noticing this with multiple cookie variables?

To sum up,

This code works.

xhr.setRequestHeader("Cookie", "cookie1=value1");

But when it comes to multiple variables, it fails.

xhr.setRequestHeader("Cookie", "cookie1=value1; cookie2=value2");

P.S. I also look at "titanium_mobile/iphone/Classes/TiNetworkHTTPClientProxy.m" seems the problem is here in "-(void)setRequestHeader:(id)args" method.

suggestions, guides, hints or solutions would be really appreciated.

Regards,
Neti.

— asked June 8th 2011 by Neti Pengkhuan
  • cookie
  • cookies
  • createhttpclient
  • setrequestheader
1 Comment

1 Answer

  • Accepted Answer

    I would suggest that you create a loop to add the cookies to the request header.

    The code you referenced shows that when the key is "cookie" it is only expecting one cookie at a time… further down in the code, it appears that it appends the cooke to the cookies object

    — answered June 8th 2011 by Aaron Saunders
    permalink
    4 Comments
    • So it should be work if I do like this, right ?

      xhr.setRequestHeader("Cookie", "cookie1=value1");

      xhr.setRequestHeader("Cookie", "cookie2=value2");

      — commented June 8th 2011 by Neti Pengkhuan
    • i believe so, based on my understanding of "-(void)setRequestHeader:(id)args"

      — commented June 8th 2011 by Aaron Saunders
    • YESS !! It works. Many thanks :)

      — commented June 8th 2011 by Neti Pengkhuan
    • In my experience, the above method works for iPhone. But on Android, doing this causes only the last cookie (last "setRequestHeader" statement) to be sent. I had to set the cookies in the following manner to make it work on both iPhone and Android:

      if (Titanium.Platform.name == 'iPhone OS')  
        {
          xhr.setRequestHeader("Cookie", "cookie1=value1");
          xhr.setRequestHeader("Cookie", "cookie2=value2");
        }
      else
        {
          var cookieString = "cookie1=value1; cookie2=value2;";
          xhr.setRequestHeader('Cookie', cookieString);
        }
      

      — commented July 9th 2011 by Y M
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.