XMLHttpRequest error with basic authentication

Any idea why the XMLHttpRequest with the correct credentials in the Pebble JS Framework fails to perform basic authentication on Android but works on iOS?

Exactly the same code, line by line:

 var req = new XMLHttpRequest(); req.open(method, url, true, user, pass); req.send(data); req.onreadystatechange = function() { ... } 

Returns 401 from an Android Pebble application, but correctly authenticates to iOS.

+6
source share
1 answer

I found a workaround that worked for me on Android.

Not sure why, but direct authenticated request:

  req.open(method, fullurl, true, user, pass); req.send(data); 

didn't work for me - it always returned 401. So instead, I tried to set basic authentication through the header:

  req.open(method, fullurl, true); req.setRequestHeader("Authorization", "Basic " + Base64.encode(user + ":" + pass)); req.send(data); 

(where Base64 is taken from here: fooobar.com/questions/5484 / ... ) - and it worked! Perhaps this is a bug in the implementation of XmlHttpRequest on Android.

+17
source

Source: https://habr.com/ru/post/970826/


All Articles