0

In QML, it seems like I can't call responseURL from my XMLHttpRequest object :

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState === 4) {
        console.log(xhttp.responseURL)
    }
};

For example, using cURL: curl -L -v http://google.com/ I get a 301 Moved Permanently response then I'm redirected to https://www.google.com/ with status 200 OK

BUT using the XMLHttpRequest I get an output of qml: undefined when I would expect it to output the url of wherever I was redirected to. I've also tried using getAllResponseHeaders() to find the Location: header, but that isn't usually returned (for security reasons I believe). So, how can I find out to which URL I was redirected?

EDIT: Included is the MRE, which returns no responseURL or location header as it is a 405 error

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480

    Component.onCompleted: {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4) {
                console.log("RESPONSE STATUS: " + xhttp.status)
                console.log("RESPONSE HEADERS: " + xhttp.getAllResponseHeaders())
                console.log("RESPONSE: " + xhttp.response)
                console.log("RESPONSE URL: " + xhttp.responseURL)
            }
        };
        xhttp.open("GET", "https://auth.ebay.com/oauth2/authorize?client_id=TylerMil-Bulba-PRD-81fa8c353-94042920&response_type=code&redirect_uri=Tyler_Miller-TylerMil-Bulba--hfgsw&scope=https://api.ebay.com/oauth/api_scope")
        xhttp.send()
    }
}

Output:

qml: RESPONSE STATUS: 405
qml: RESPONSE HEADERS: date: Fri, 20 Dec 2019 18:55:18 GMT
content-type: text/html; charset=utf-8
transfer-encoding: chunked
connection: keep-alive
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
rlogid: t6pbhnmpo%3D9iptpbhnmpo*0321%3A4%3E-16f24a9fe8c-0xb06
vary: Accept-Encoding, Accept-Encoding
x-distil-cs: MISS
server: eBayServer
expires: Thu, 01 Jan 1970 00:00:01 GMT
cache-control: private, no-cache, no-store, must-revalidate
edge-control: no-store, bypass-cache
surrogate-control: no-store, bypass-cache
strict-transport-security: max-age=31536000, max-age= 31536000
qml: RESPONSE: <!DOCTYPE html><html lang="en" style="overflow: visible;"><head><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta charset="utf-8"><meta name="google-site-verification" content="RDpd-WPXK6QX0AeQPo4hCtBF-2nKyj3zjzY-H7GgRs8"><meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="https://pages.ebay.com/favicon.ico"><title>Security Measure</title> 
******removed much of the response body******
</footer></body></html><!-- RcmdId distil_captcha,RlogId t6pujfpmsn5%3F%3Ckuvuwoduovl0*%3B34561-16f24a9fea4-0x2b02 --><!-- SiteId: 0, Environment: production, AppName: splashui, PageId: 2557882 -->
qml: RESPONSE URL: undefined
Tyler M
  • 352
  • 4
  • 21
  • Does this answer your question? [Is it possible for XHR HEAD requests to not follow redirects (301 302)](https://stackoverflow.com/questions/3820663/is-it-possible-for-xhr-head-requests-to-not-follow-redirects-301-302) – folibis Dec 20 '19 at 17:35
  • @folibis no, unforunately that does not answer the question. The question you linked says that redirects *must* be followed by the browser, which I understand -- and one commenter says that `responseURL` is an appropriate method to find out where the browser redirected. But as I show in my example above, `responseURL` returns undefined for me in a QML environment – Tyler M Dec 20 '19 at 18:17
  • Please provide [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and some real URL if possible so we can test it – folibis Dec 20 '19 at 18:21
  • @folibis not sure how I missed this, but the `location` header *is* returned in the `getAllResponseHeaders()`... I discovered this when I was building an MRE. That solves half of my questions. I've posted the MRE - I'm still not getting any `location` under certain conditions, like when i receive a `405 Not Allowed`, but is that just the nature of `405` to not include a URL? – Tyler M Dec 20 '19 at 18:54

0 Answers0