Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Problem getting responseText/headers from error response

Hi guys,

My server app feeds back a custom HTTP header (and response) on 401 code responses. Using Titanium's HTTPClient, I can't seem to be able to get the response from the server (the console shows everything I print in the example below as null). Any ideas?

var xhr = Titanium.Network.createHTTPClient({
    'timeout': 5000
});


//Tried this method, no luck
xhr.onreadystatechange = function() {
        Titanium.API.info('Readystate, got status ' + this.status + ': ' + this.response);
        Titanium.API.info('Readystate, got status ' + this.status + ': ' + this.responseText);
        Titanium.API.info('Readystate BLA: ' + this.getResponseHeader('X-Custom-Header'));
    };
                Titanium.API.info('Readystate BLA: ' + xhr.getResponseHeader('X-Custom-Header'));
    };


//Also tried this
xhr.onerror = function() {
    if (this.status == 401) {
        //auth challenge required
        var guid = xhr.getResponseHeader('X-Challenge-GUID');
        Titanium.API.info('Server gave GUID challenge of: "' + guid + '"');
        Titanium.API.info('TEST FOO: ' + xhr.getResponseHeader('X-Custom-Header'));
        Titanium.API.info('TEST BAR: ' + this.getResponseHeader('Cache-Control'));
        Titanium.API.info('TEST BLA: ' + xhr.responseText);
        Titanium.API.info('TEST BOO: ' + this.responseText);
    }
}

xhr.open("GET", url);
xhr.send();

So I turn on one or the other of what's above. Processing of response data with onload works fine, it's just there's nothing when I'm trying to get it from a 401 "error" response. For instance:

[INFO] Readystate, got status 0: undefined
[INFO] Readystate, got status 0: null
[INFO] Readystate BLA: null
[INFO] Readystate, got status 0: undefined
[INFO] Readystate, got status 0: null
[INFO] Readystate BLA: null
[INFO] Readystate, got status 0: undefined
[INFO] Readystate, got status 0: null
[INFO] Readystate BLA: null
[INFO] Readystate, got status 401: undefined
[INFO] Readystate, got status 401: null
[INFO] Readystate BLA: null
[INFO] Simulator is exiting

Thanks,

Robby

— asked August 6th 2010 by Robby Dermody
  • httpclient
  • onerror
  • onreadystate
0 Comments

8 Answers

  • Just as an additional point, I've verified (using the LiveHTTPHeaders plugin for Firefox) that I'm including the 'X-Challenge-GUID' header in the 401 response, as well as data in the response body itself.

    After messing with this for 5+ hours, I've created a ticket for this issue, as it appears to be a bug:

    https://appcelerator.lighthouseapp.com/projects/32238-titanium-mobile/tickets/1502-httpclient-problems-with-header-parsing-and-responsedata-in-onerror-handler

    — answered August 9th 2010 by Robby Dermody
    permalink
    0 Comments
  • This is definitely a bug. I had to deal with the same problem. Seems like a major one too because it's impossible to get any headers from responses.

    There's actually a lighthouseapp bug that seems to deal with it. I don't remember the exact description, but it has something to do with deligates, etc. The resolution of that bug keeps getting pushed back over and over again. It's strange that such a major HTTPClient bug has such a low priority.

    — answered August 9th 2010 by Kyle Quest
    permalink
    0 Comments
  • My apps use httpclient hugely so am at the mo' hold with v1.3.3, till I hear this is Ok

    (Edit: oops, could've sworn I added as comment)

    — answered August 10th 2010 by Chris Reed
    permalink
    0 Comments
  • As workarounds to these issues, I had to change my server code to use 403 (Forbidden) to ask for auth, and had to change the client to pass the auth username and password in as GET parameters. That, and I had to change the server to not pass any info in HTTP headers (it seems that sometimes – only for non-error http codes – certain custom headers that don't start with X- work, but other times they don't…things as simple as changing the case seem to impact that).

    AND, I put everything in onreadystatechange, because I don't trust onload/onerror: Reason is because a 401 error shows up in onerror, and a 403 error shows on in onload….??? This means that I can get data out of the body with 403 errors, but not with 401 errors. So, instead of using headers, I just pass the challenge token info in the body, and because a 403 is "not an error", I can read it out as this.responseText.

    What a kludge. Conclusion: Httpclient is MAJORLY messed up in 1.4.0

    — answered August 10th 2010 by Robby Dermody
    permalink
    0 Comments
  • Tried running 1.3.3 code in 1.4.0, HTTPClient.onload seems broken in 1.4.0

    — answered August 30th 2010 by Chris Reed
    permalink
    0 Comments
  • hi

    this is still bugged in SDK 1.7.0RC

    cant access custom headers.

    i've done a hack and included my custom values into Content-type header so ti looks like:

    "text/html; charset=utf-8; my values"

    — answered April 27th 2011 by gondo gondo
    permalink
    0 Comments
  • hi

    this is still bugged in SDK 1.7.0RC

    cant access custom headers.

    i've done a hack and included my custom values into Content-type header so ti looks like:

    "text/html; charset=utf-8; my values"

    — answered April 27th 2011 by gondo gondo
    permalink
    0 Comments
  • Any update on this? I still can't get 401 response back from the server. 200 and 406 are fine.

    — answered May 17th 2011 by o o
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.