Titanium Community Questions & Answer Archive

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

How to do a http PUT with HTTP Basic Authentication

I tried and tried but doesn't seem to get any results:

var xhr = Titanium.Network.createHTTPClient();
xhr.onreadystatechange = function() {
try {
if (this.readyState == 4)
{
var results = JSON.parse(this.responseText);
}
} catch(e)
{
Ti.API.debug(e.error);
Titanium.UI.createAlertDialog({
title: "Error",
message: String(e.error),
buttonNames: ['OK']
}).show();
}
};

xhr.open("PUT","https://" + username +":" + password + "@" + UA_URL + UA_Server + api);

xhr.setTimeout([1000]);
xhr.setRequestHeader("Content-Type","application/json");

xhr.send();

— asked April 29th 2010 by Peter Lum
  • authentication
  • basic
  • http
  • put
0 Comments

2 Answers

  • You need to base64 encode the username and password, and send them as a seperate header for basic auth to work:

    authstr = 'Basic ' +Titanium.Utils.base64encode(username+':'+password);
    xhr.setRequestHeader('Authorization', authstr);

    — answered April 29th 2010 by Nick Pack
    permalink
    1 Comment
    • Thanks! I've been bashing my head over this for ages. It's worth mentioning though that you have to call setRequestHeader() AFTER calling open() but BEFORE send()

      — commented July 14th 2010 by Mark Biek
  • Hi there Peter,

    Don't have anything to add right now, but today I'm thrashing-out how to add PUTs and POSTs to my app, will feedback what I find works.

    cheers,
    Chris.

    — answered April 29th 2010 by Chris Reed
    permalink
    0 Comments
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.