Titanium Community Questions & Answer Archive

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

Basic Authentication within createHTTPClient

happy friday!

Hey I was wondering if there is a basic authentication method for Titanium.Network.createHTTPClient(); as there is for Titanium.UI.createWebView.

I need to connect with a server that has a basic apache auth box that pops up and asks for u/p

I know I can use: http://USER:PASS@URL but I think thats a bit less secure right?

I didnt see anything in the documentation but thought I would ask. Thanks

— asked September 24th 2010 by Dave F
  • authentication
  • createhttpclient
0 Comments

1 Answer

  • Accepted Answer

    Last time I checked there wasn't any built-in support for basic auth in HTTPClient. Fortunately, you can easily set the correct headers yourself:

    xhr.setRequestHeader(
        'Authorization', 
        'Basic ' + Ti.Utils.base64encode(username+':'+password));
    
    — answered September 24th 2010 by Carmen Wick
    permalink
    4 Comments
    • Thats really cool. I didnt know what was possible. I gave it a shot but the error returned is Error.. Authentication Needed. Is there a reason this wouldnt work but the USERS:PASS@URL method I mentioned above would work? Thanks for the help!

      — commented September 24th 2010 by Dave F
    • You may have to call setRequestHeader() after you call open(), but before you call send(). See examples/foursquare.js in the KitchenSink. Also try sniffing the HTTP traffic to see if the Authorization header is being set correctly. Some good tools for this: Fiddler for windows (free), and HTTPScoop for OSX (costs $, but trial is free).

      — commented September 24th 2010 by Carmen Wick
    • Oh man! that worked!!! you're the best! Thanks so much!!

      — commented September 24th 2010 by Dave F
    • This helped me immensely too! Thanks!!

      — commented September 9th 2013 by Paulo Dichone
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.