c++qtqnetworkaccessmanagerqnetworkreplyqnetworkrequest

qnetworkreply no statuscode or error but failing


Im trying to find out what goes wrong when doing a get request in qt. I have the following slots attached to my networkmanager:

connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(requestFinished(QNetworkReply*)));

    connect(mgr, SIGNAL(error(QNetworkReply::NetworkError)),
            this, SLOT(slotError(QNetworkReply::NetworkError)));

The request finished is like:

    void FirebaseInteractor::requestFinished(QNetworkReply *rep)
    {
        QVariant statusCode = rep->attribute( QNetworkRequest::HttpStatusCodeAttribute );
        int status = statusCode.toInt();
        if ( status != 200 )
        {
            QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
            qDebug() << "Pushnotification Request failed : " <<reason;
        }
        else{
            qDebug() << "Pushnotification has been send: ";
        }
        if ( !statusCode.isValid() )
        {

            QString status = statusCode.toString(); // or status_code.toInt();
            qDebug() << "Failing " << status;
            int code = statusCode.toInt();
            qDebug() << "Pushnotification Request failed invalid status code." << QString::number(code);
            QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
            qDebug() << "reason " << reason;
            return;
        }
    }

However Status is always empty Failing and reason is printed but there is no value after it (i was expecting a reason e.g. timeout, 401 etc).

I also tried:

    int status = statusCode.toInt();

    if ( status != 200 )
    {
        QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
        qDebug() << "Pushnotification Request failed : " <<reason;
    }
    else{
        qDebug() << "Pushnotification has been send: ";
    }

But all reasons/codes are empty.

I also added:

    void FirebaseInteractor::slotError(QNetworkReply::NetworkError error)
    {
        qDebug() << "slotError" << error;
    }

but this is not called.

How can I find out whats going wrong?


Solution

  • You can get the error directly using:

    qDebug() << reply->error();
    

    This won't work if the network request never happened:

        if ( !statusCode.isValid() ) 
    

    Because this means that the QVariant itself is invalid and has type QMetaType::UnknownType Documentation. Hence it will not give any information about what went wrong in the http request. To fix this, here's a simple example:

    if (statusCode.isValid()) { // it needs to be valid
        qDebug() << "Status Code: " << statusCode.toString();
    }
    

    Alternatively, you can switch on QNetworkReply::Error()

    void FirebaseInteractor::requestFinished(QNetworkReply *rep)
    {
        switch (rep->error())
        {
           case QNetworkReply::NoError:
           // No error
           return;
           case QNetworkReply::TimeoutError:
           {
               auto httpStatus =
                   reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
               auto httpStatusMessage = reply->attribute(
            QNetworkRequest::HttpReasonPhraseAttribute).toByteArray();
            //...
            break;
           }
        }
    }