How to do a http PUT with HTTP Basic Authentication

You must Login before you can answer or comment on any questions.

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 4 years ago by Peter Lum
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 4 years ago by Nick Pack
answer 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 4 years ago 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.

Your Answer

Think you can help? Login to answer this question!