Set header to webEngine request QML

49 Views Asked by At

Is it possible to set a header to a QML WebEngineView when we change the URL? We perform a magic link and obtain a bearer token, which we must send as an "authorization" header to the URL. However, I am unable to understand how to attach the header to the WebEngineView.

I tried to use XMLHttpRequest::setRequestHeader, but when I receive a response and try to load it in the WebEngineView using loadHtml(), it appears that some scripts in XMLHttpRequest::responseText are not executed, resulting in only a part of the webpage being displayed.

function send(customurl, bearer) {
    // Create a custom XMLHttpRequest object
    var xhr = new XMLHttpRequest()
    xhr.withCredentials = true
    // Define the URL
    var webEngineUrl = customurl

    // Open a GET request with the URL
    xhr.open("GET", customurl)

    // Set the Bearer token in the Authorization header
    xhr.setRequestHeader("Authorization", bearer)

    xhr.setRequestHeader("User-Agent", "DP Desktop App")
    // Handle response
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                // Request successful, handle response data
                webEngineView.loadHtml(xhr.responseText,
                                       customurl)
            } else
                console.error(
                            "Failed to open temporary file for writing")
        } else {
            // Error handling
            console.error("Error:", xhr.status, " ",
                          xhr.statusText)
        }
    }

    // Send the request
    xhr.send()
}

Is there any other way to set header?

0

There are 0 best solutions below