Hello all,
apparently the base64encode method per default inserts line breaks into strings which encoded are longer than 72 chars, which may not always be desirable. Example:
var xhr = Ti.Network.createHTTPClient(); xhr.open('GET', 'http://some.resource.com'); authstr = Titanium.Utils.base64encode(username.value + ':' + password.value).toString(); // If username and password are long, the resulting encoded string will be long, // and a line break will be inserted after the 72nd character // Host requires HTTP Basic Authentication xhr.setRequestHeader('Authorization', 'Basic ' + authstr); xhr.send();The problem here is that xhr.setRequestHeader will not work because of the line break. The Authorization header will not be set, and the host will not recognize the user and send a 401 back.
A workaround is to remove the line breaks from the string:
authstr.replace(/(\r\n|\n|\r)/gm,"");However, in my opinion the base64encode method should at least provide the option to return the string without line breaks. It seems that the default way of using base64 encoding is with line breaks, but usually it's possible to get the line breaks removed directly in the encoding as well. The way it is right now, headers mysteriously get lost and it takes quite a lot of debugging with HTTP sniffers and trial/error coding to work out why.
I have only tested this on iPhone, not on Android, but I suspect this is an all-round issue.
1 Answer
Of course, the bug is probably not so much in the fact that the base64encode function is too simple and more in the fact that you can pass a value to the setRequestHeader function and it will just silently fail without so much as a warning.
I'd love to see a HTTP sniffer built into the Titanium Developer by the way - with one of those I could have found the error a lot sooner than I did.
Your Answer
This question has been locked and cannot accept new answers.