Ti.Network.createHTTPClient 401 without credentials: retry behavior?
0 reputation · 23 Oct 2022, 01:26 UTC
0 reputation · 23 Oct 2022, 01:26 UTC
When a Titanium application calls Ti.Network.createHTTPClient to reach a Basic Auth protected API without supplying an Authorization header, the request returns a 401 status code. The SDK’s documentation does not specify whether it attempts a retry or simply fails.
The goal is to determine if the client automatically retries the request after the initial 401 or if it fails immediately, and whether this behavior differs between iOS and Android in the current stable release.
Key constraints: no credentials are provided; the test must be performed in a clean environment without any prior authentication state.
Questions:
Ti.Network.createHTTPClient automatically retry a request after receiving a 401 when no Authorization header is present?In the current stable Titanium SDK, Ti.Network.createHTTPClient does not automatically resend a request after receiving a 401 when no Authorization header is supplied. The HTTP client simply delivers the 401 response to the callbacks you registered.
No SDK option or callback can turn on a retry for authentication failures. The only way to retry is to detect the 401 in your code (via onload or onerror), add the appropriate Authorization header, and call send() again.
Both platforms use the same Titanium wrapper logic. Neither wrapper performs a hidden retry, although the timing of the onload versus onerror callback may differ slightly between the two OSes. In practice, you will see exactly one request from the app on each platform.
express-basic-auth).createHTTPClient without an Authorization header:var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
Ti.API.info('onload status: ' + e.source.status);
},
onerror: function(e) {
Ti.API.info('onerror: ' + e.error + ', status: ' + e.source.status);
}
});
xhr.open('GET', 'https://your‑server.com/protected');
xhr.send();
Once you receive the 401, build the Authorization header and resend:
function retryWithAuth() {
var auth = 'Basic ' + Ti.Utils.base64encode('user:pass');
xhr.setRequestHeader('Authorization', auth);
xhr.send();
}
Call retryWithAuth() from within your onload or onerror handler when the status is 401.
To fine‑tune your retry logic, it helps to know whether the server logs show a second request after the 401. If you observe a second request, that would indicate a platform‑specific retry you are unaware of.
Use comments to ask for clarification. Post a solution as an answer.
5,300 reputation · 23 Oct 2022, 06:54 UTC
One clarification worth testing: a 401 does not always route to onerror. Depending on the Titanium SDK version and native stack, the response may reach onload with e.source.status === 401, while onerror handles transport failures. Relying only on onerror can miss the authentication failure entirely.
Version assumption: this callback routing is not guaranteed across stable releases, so inspect status in both handlers on your target SDK. A local proxy should still show exactly one request; if you see a second, it is likely from your own retry logic or a redirect, not an automatic 401 resend by the client.