getResponseHeader doesn't return all fields



  • Hi,
    i'm using var xhr = new XMLHttpRequest(); to do a post request, when i read the response header i don't find all the fields (i.e. Autohorization). Using the c++ classes i can read all the fields. What's the problem with js function?
    N.


  • Moderators

    @niqt
    you need to be more specific.

    i assume you are using QML / QQmlEngine? Or do you use QtScript?
    Whats the url you are posting to? A custom web server/service?
    Show some code please, maybe you are reading the response too early.



  • My code it's in js file and i call it from qml

    function requestRestUser(verb, endpoint, obj, cb) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
    
            if(xhr.readyState === XMLHttpRequest.DONE) {
                if(cb) {
                    if (xhr.status == "200") {
                        console.log("header" + xhr.getAllResponseHeaders());
                        cb(true, xhr.getResponseHeader('Authorization'));
                    } else if (xhr.status == "400" || xhr.status == "401") {
                        cb(false, "");
                    }
                }
            }
        }
        xhr.open(verb, endpoint);
        xhr.setRequestHeader('Content-Type', 'application/json');
        var data = (obj)?JSON.stringify(obj):''
        xhr.send(data)
    }
    

    I have 200 code, but the xhr.getResponseHeader('Authorization') is empty (if i do the same with c++ i got it). The remote server ia java rest server.


  • Moderators

    @niqt
    even though QML's XMLHttpRequest object doesn't enforce the same origin policy, it may be still related to such or similar.
    In case i am right and it's a CORS related issue you might want to try to add the following:

    xhr.setRequestHeader('Access-Control-Expose-Headers', 'Authorization');
    

    Can you sniff the HTTP requests and corresponding responses done with XMLHttpRequest and also with QNetworkAccessManager and post them here? For sniffing you can use a tool like Wireshark for example.

    Maybe your Java webservice doesn't like something about the request sent via XMLHttpRequest. But this should be clearly seen when comparing the Request- and Response-Headers.



  • Can be a problem https?


  • Moderators

    @niqt said in getResponseHeader doesn't return all fields:

    Can be a problem https?

    shouldn't be, since the data after all should be the same.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.