Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not getting any response from post request in form url encoded format
Qt 6.12 LTS is out! See what's new in the release blog

Not getting any response from post request in form url encoded format

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    QNetworkAccessManager works asynchronously. You need to use signals and slots to properly handle the request and its answer,

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • N Offline
      N Offline
      ndiwan
      wrote on last edited by ndiwan
      #3

      Hi SGaist,

      Do I need to do like this:

      void HttpWorker::onPostAnswer(QNetworkReply* reply)
      {
      int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
      if(statusCode == 200)
      {
      const QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
      const QJsonObject response_json = QJsonDocument::fromVariant(response.object().value("smsession").toVariant()).object();
      }
      }

      void HttpWorker::GetSiteminderToken()
      {
      QNetworkReply* reply;
      QNetworkRequest request(QUrl("https://xxtest.employees.www.uprr.com/admin/login.fcc"));
      request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

      QUrlQuery postArray;
      postArray.addQueryItem("TARGET" , "https://xxtest.employees.www.uprr.com/hmp/alert-handler/1.0/secure/jas/fetch-session-cookie");
      postArray.addQueryItem("USER" , "abcd1234");
      postArray.addQueryItem("PASSWORD" , "rfte123");
      

      QNetworkAccessManager manager = new QNetworkAccessManager(this);
      connect(manager, SIGNAL(finished(QNetworkReply
      )),
      SLOT(onPostAnswer(QNetworkReply*)));
      reply = manager->post(request, postArray.toString(QUrl::FullyEncoded).toUtf8());

      }

      But Still status code is 0. It should be 200.

      Thanks

      JonBJ 1 Reply Last reply
      0
      • N ndiwan

        Hi SGaist,

        Do I need to do like this:

        void HttpWorker::onPostAnswer(QNetworkReply* reply)
        {
        int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        if(statusCode == 200)
        {
        const QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
        const QJsonObject response_json = QJsonDocument::fromVariant(response.object().value("smsession").toVariant()).object();
        }
        }

        void HttpWorker::GetSiteminderToken()
        {
        QNetworkReply* reply;
        QNetworkRequest request(QUrl("https://xxtest.employees.www.uprr.com/admin/login.fcc"));
        request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

        QUrlQuery postArray;
        postArray.addQueryItem("TARGET" , "https://xxtest.employees.www.uprr.com/hmp/alert-handler/1.0/secure/jas/fetch-session-cookie");
        postArray.addQueryItem("USER" , "abcd1234");
        postArray.addQueryItem("PASSWORD" , "rfte123");
        

        QNetworkAccessManager manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply
        )),
        SLOT(onPostAnswer(QNetworkReply*)));
        reply = manager->post(request, postArray.toString(QUrl::FullyEncoded).toUtf8());

        }

        But Still status code is 0. It should be 200.

        Thanks

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @ndiwan
        I don't know, but maybe you should slot onto QNetworkReply::errorOccurred signal?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Please use coding tags around your code to make it readable (the </> button on the toolbar).

          That said the connect statement looks wrong since you have once a pointer for the signal and the other an object for the slot.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • N Offline
            N Offline
            ndiwan
            wrote on last edited by ndiwan
            #6

            void HttpWorker::postFinished(QNetworkReply* rep)
            {
            QByteArray bts = rep->readAll();
            QString str = QString::fromUtf8(bts);
            qDebug().noquote() << "Reply:"<<str;
            rep->deleteLater();
            }

            QString HttpWorker::GetSiteminderToken()
            {

            QNetworkReply* reply;
            QNetworkRequest request(QUrl("https://xxtest.employees.www.uprr.com/admin/login.fcc"));
            request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
            
            QUrlQuery postArray;
            postArray.addQueryItem("TARGET" , "https://xxtest.emp->oyees.www.uprr.com/hmp/alert-handler/1.0/secure/jas/fetch-session-cookie");
            postArray.addQueryItem("USER" , "abcd1234");
            postArray.addQueryItem("PASSWORD" , "frtg4567");
            QNetworkAccessManager *manager = new QNetworkAccessManager(this);
            connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(postFinished(QNetworkReply*)));
            manager->post(request, postArray.toString(QUrl::FullyEncoded).toUtf8());
            

            }

            Still getting str as "" .

            Thanks

            1 Reply Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #7
              This may be helpful. Try to check your return in postFinished().
              m_reply = m_networkAccessManager.post( request, byte_array );
              if ( nullptr != m_reply ) {
                  if ( m_reply->isRunning() ) {
                      connect( m_reply, &QNetworkReply::finished,
                               this,    &MyClass::postFinished );
                  }
                  else { /* may be finished immediately*/
                      postFinished();
                  }
              }
              
              1 Reply Last reply
              0
              • N Offline
                N Offline
                ndiwan
                wrote on last edited by
                #8

                Sorry that didnt work.

                JonBJ 1 Reply Last reply
                0
                • N ndiwan

                  Sorry that didnt work.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @ndiwan
                  Put a breakpoint on entry to onPostAnswer(). Use the watch window to have a dig around in *reply to see what you can see there! Anything interesting? You say returned state code is 0, which I don't think is a code HTTP would return at all. I did also suggest you verify you do not get any error.

                  1 Reply Last reply
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #10

                    print out the error message while m_reply has to be global in the class.
                    void MyClass::postFinished()
                    {
                    if ( nullptr != m_reply ) {
                    if ( QNetworkReply::NoError == m_reply->error() ) { /* update is successful */
                    }

                    1 Reply Last reply
                    0
                    • JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #11

                      Try your QUrl with the following format:
                      http://user:password@domain.com/

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        ndiwan
                        wrote on last edited by
                        #12

                        HI All,

                        If you notice the body of my request, it has a target url that will return a cookie. That cookie should append to the final post request.
                        So is there any other way to handle cookies ?

                        Thanks

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt
                        • Unsolved