Getting the HTTP standard response code from a QNetworkReply

  • I am working on some code to get an image from a web server using QNetworkReply. Since there is security to be concerned with, I need to look at the HTTP standard response code to determine if the result is a clean 200 or some type of error. How exactly do I get the the HTTP standard response code?

  • Have you tried "error()": method?

  • The raw response code is available as an integer using the QNetworkReply::attribute(QNetworkRequest::HttpStatusCodeAttribute) method.

  • Connect a slot up to the QNetworkReply::metaDataChanged() signal. Then in that slot you can check what you need:

    QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
    if ( !statusCode.isValid() )

    int status = statusCode.toInt();
    if ( status != 200 )
        QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
        qDebug() << reason;


Log in to reply