Nominate our 2022 Qt Champions!

Getting status code 0 for HTTP post request in qt 5.2

  • Hi,
    I have built the web services in Ruby on Rails returning Json data, I have checked it through chrome addin and it is working good, returning correct status code on success. So, here I am trying to access these web service but I am getting wrong status code which is 0. Here is my code...but I could not find where I did wrong as I tried to find the problems with logs.

    @ QNetworkAccessManager networkManager;
    QNetworkAccessManager mgr;
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    qDebug() << "Called the C++ slot with message:sssss";
    QUrl url;
    QByteArray postData;
    QNetworkRequest request(url);

        QString postKey = "email";
        QString postValue = "";
        QString postKey1 = "password";
        QString postValue1 = "abcde12345";
        QNetworkReply *reply=,postData);
        QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
        int status = statusCode.toInt();
        qDebug() << "Called the C++ slot with message:Correct" << status;
        if ( status==200 )
            qDebug() << "Called the C++ slot with message:Correct";
        if ( status != 200 )
            QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
            qDebug() << reason;


  • Lifetime Qt Champion


    If I see things correctly, you don't wait your post to complete before analyzing the reply

  • wait to complete post means shall I put some counter before starting analyzing the reply..correct me if I did not understand your point well.

  • Lifetime Qt Champion

    Use the finished signal and analyze the result in a slot connected to it

Log in to reply