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

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;
    url.setUrl("http://localhost:3000/api/v1/sessions");
    QNetworkRequest request(url);

        QString postKey = "email";
        QString postValue = "abcd@abcd.com";
        QString postKey1 = "password";
        QString postValue1 = "abcde12345";
    
        postData.append(postKey).append("=").append(postValue).append("&").append(postKey1).append("=").append(postValue1);
        QNetworkReply *reply= networkManager.post(request,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";
        return;
    
        if ( status != 200 )
        {
            QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
            qDebug() << reason;
        }
    
    }@
    

    Thanks...


  • Lifetime Qt Champion

    Hi,

    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