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();
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); -
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.