401 error with cordova android

It doesn’t look like “fetch” is altering the url. The difference between fetch and coax, is that coax is sending the following request headers:

GET /todo HTTP/1.1
Host: localhost:5985
Connection: keep-alive
Authorization: Basic MGE2MDUzNDktZmQyOS00NTYxLTg1YjEtZDFjOWE0Y2JiMDBkOmY0NzBkOTFhLThhMzYtNDliNi05YTZiLThiYWE5YzEwNzc4ZA==
accept: application/json
X-DevTools-Emulate-Network-Conditions-Client-Id: 036156A7-631B-43A3-863C-621DB9CF49AD
User-Agent: Mozilla/5.0 (Linux; Android 6.0; LG-H810 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.106 Mobile Safari/537.36
content-type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US
X-Requested-With: com.couchbase.TodoLite`

Whereas “fetch” only sends

User-Agent: Mozilla/5.0 (Linux; Android 6.0; LG-H810 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko)

Looks like the ability to generate the “Authorization” header is a specific function in coax. I found it in modules.js, line 1280 of the Todo-list app:

options.headers.authorization = 'Basic ’ + b64_enc(options.auth.username + ‘:’ + options.auth.password);

Aha, that must be it. (Sorry, I thought that XHR would recognize the credentials in the URL, but apparently not.) thanks for figuring this out.

We’ll need to clarify this for people using Cordova on Android, I.e. that not all JS libraries work. Or else provide our own API for sending requests.

It appears to be a CORS issue with fetch. I’ll post the solution once I have it working.

@jens and @hideki thanks for your help.

I’ve filed an issue against the Cordova plugin. We need to at least document clearly that you have to send the Authorization header (on Android).

I don’t think it’s a CORS issue, just that fetch doesn’t add the Authorization: header.

Here’s the solution for fetch:

fetch(dbUrl, {credentials: ‘include’})

Not exactly sure what is causing the 401 errors, but I do have a working reference cordova app that is not using coax and doesn’t have any interaction issues with CBL. Perhaps it will help troubleshoot.

May I ask that people continue the discussion at the Github issue? That will help us keep the information organized and find a solution. Thanks.