Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QNetworkRequest basic Authentication



  • My curl command looks like this:

    curl --user 'user:password' --data '{"jsonrpc": "1.0", "id":"curltest", "method": "list", "params": [6, 9999999, [] , true, { "minimumAmount": 0.005 } ] }' -H 'content-type: application/json;' http://127.0.0.1:8332
    

    If I use this curl command from the console it works fine.

    For QNetworkManager I tried like this:

    QByteArray userAndPassword;
    userAndPassword.append(m_rpcUser);
    userAndPassword.append(":");
    userAndPassword.append(m_rpcPassword);
    
    m_networkRequest.setRawHeader("Authorization", "Basic " + userAndPassword);
    m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    

    I always get the following error:

    Error QNetworkReply::AuthenticationRequiredError "Der Host verlangt eine Authentifizierung"
    

    How can I set the basic authentication for a QNetworkRequest?


  • Lifetime Qt Champion

    You forgot to encode the credentials.

    See here.



  • @SGaist said in QNetworkRequest basic Authentication:

    You forgot to encode the credentials.

    Now it looks like this:

    QString credentialsString = QString("%1:%2").arg(m_rpcUser).arg(m_rpcPassword);
    QByteArray credentialsArray = credentialsString.toLocal8Bit().toBase64();
    QString headerData = "Basic " + credentialsArray;
    
    m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
    

    But I still get the same error.


  • Lifetime Qt Champion

    One other thing you can use is the QNAM::authenticationRequired signal and fill the details there.



  • @SGaist said in QNetworkRequest basic Authentication:

    One other thing you can use is the QNAM::authenticationRequired signal and fill the details there.

    With this I don't get the error anymore, but the the request is not called anymore.

    void RpcEndPoint::on_networkManagerAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
    {
        authenticator->setUser(m_rpcUser);
        authenticator->setPassword(m_rpcPassword);
    
        m_reply = reply;
    
        // Connect the slots to the new m_reply
        connect(m_reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
            [=](QNetworkReply::NetworkError code) {
                qDebug() << "Error" << code << m_reply->errorString();
            }
        );
    
        connect(m_reply, &QNetworkReply::downloadProgress,
                this, &RpcEndPoint::on_downloadProgress);
    
        connect(m_reply, &QNetworkReply::readyRead,
                this, &RpcEndPoint::on_downloadReadyRead);
    
        connect(m_reply, &QNetworkReply::finished,
                this, &RpcEndPoint::on_downloadFinished);
    
    }
    


  • @Infinity said in QNetworkRequest basic Authentication:

    m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());

    It looks like you're missing the final ":" (colon) for the header, as it should be "Authentication:" see RFC 7617.



  • @Pablo-J-Rogina said in QNetworkRequest basic Authentication:

    It looks like you're missing the final ":" (colon) for the header, as it should be "Authentication:" see RFC 7617.

    QString credentialsString = QString("%1:%2").arg(m_rpcUser).arg(m_rpcPassword);
    
    QByteArray credentialsArray = credentialsString.toLocal8Bit().toBase64();
    
    QString headerData = "Basic " + credentialsArray;
    
    m_networkRequest.setRawHeader("Authentication:", headerData.toLocal8Bit());
    

    Still the same error with this



  • I found the following solution:

    m_endPointUrl.setUserName(m_rpcUserName);
    m_endPointUrl.setPassword(m_rpcPassword);
    
    m_networkRequest.setUrl(m_endPointUrl);
    m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"))
    
    m_reply = m_networkManager->post(m_networkRequest, requestJsonDocument.toJson(QJsonDocument::Compact));
    

    But I still would like to know how it would work with:

    QNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
    

Log in to reply