Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access data sent with post/put from QNetworkReply
Forum Updated to NodeBB v4.3 + New Features

Access data sent with post/put from QNetworkReply

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 9 Posters 6.0k Views 5 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.
  • J Offline
    J Offline
    Jiloc
    wrote on last edited by Jiloc
    #1

    Hello everyone,

    does someone know how to access the data that I sent with a post or put request from the reply?

    for example when the finished signal is fired and the connected slot is called how can I access the data that I originally sent?

    Whathever::Whathever() 
    { 
        m_nam = new QNetworkAccessManager;
        connect(m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
    }
    
    void Whathever::testPost()
    {
        QNetworkRequest req("http://whatever.com");
        QByteArray payload("test=test");
        m_nam->post(req, payload);
    }
    
    void Whathever::onFinished(QNetworkReply *reply)
    {
        // Here i want to access the data contained in "payload"
    }
    
    C 1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by artwaw
      #2

      Hi, have read the docs on QNetworkReply? There is request() method that provides information you are looking for.

      For more information please re-read.

      Kind Regards,
      Artur

      J 1 Reply Last reply
      1
      • artwawA artwaw

        Hi, have read the docs on QNetworkReply? There is request() method that provides information you are looking for.

        J Offline
        J Offline
        Jiloc
        wrote on last edited by
        #3

        @artwaw Yep I did, but I am not asking for the request, I am asking for the payload. It is not contained in the same object.

        jsulmJ 1 Reply Last reply
        0
        • J Jiloc

          @artwaw Yep I did, but I am not asking for the request, I am asking for the payload. It is not contained in the same object.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Jiloc What about:

          void Whathever::onFinished(QNetworkReply *reply)
          {
              // Here i want to access the data contained in "payload"
              reply->readAll();
          }
          

          ?
          http://doc.qt.io/qt-5/qnetworkreply.html
          http://doc.qt.io/qt-5/qnetworkaccessmanager.html
          "QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.)."

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Jiloc What about:

            void Whathever::onFinished(QNetworkReply *reply)
            {
                // Here i want to access the data contained in "payload"
                reply->readAll();
            }
            

            ?
            http://doc.qt.io/qt-5/qnetworkreply.html
            http://doc.qt.io/qt-5/qnetworkaccessmanager.html
            "QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.)."

            J Offline
            J Offline
            Jiloc
            wrote on last edited by
            #5

            @jsulm Hello and thank you for the answer. Unfortunally readAll contains the body of the response, it's not the body of the request

            jsulmJ 1 Reply Last reply
            0
            • J Jiloc

              @jsulm Hello and thank you for the answer. Unfortunally readAll contains the body of the response, it's not the body of the request

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Jiloc Please read documentation: http://doc.qt.io/qt-5/qnetworkreply.html#request

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jiloc
                wrote on last edited by
                #7

                @jsulm As I wrote 2 posts above ,I already did. The QNetworkRequest doesn't contains the PAYLOAD of the request. In fact when you send a post or put request you set 2 different parameters in the QNetworkAccessManager methods.

                http://doc.qt.io/qt-5/qnetworkaccessmanager.html#post-1

                1 Reply Last reply
                0
                • sneubertS Offline
                  sneubertS Offline
                  sneubert
                  wrote on last edited by
                  #8

                  What about using a lambda? You could capture the payload.

                  QNetworkAccessManager *m_nam = new QNetworkAccessManager;
                  QNetworkRequest req(QUrl("http://whatever.com"));
                  QByteArray payload("test=test");
                  QNetworkReply *reply = m_nam->post(req, payload);
                  connect(reply,&QNetworkReply::finished,this,[&](){
                    qDebug() << payload;
                    qDebug() << reply->readAll();
                    reply->deleteLater();
                  });
                  
                  J 1 Reply Last reply
                  1
                  • sneubertS sneubert

                    What about using a lambda? You could capture the payload.

                    QNetworkAccessManager *m_nam = new QNetworkAccessManager;
                    QNetworkRequest req(QUrl("http://whatever.com"));
                    QByteArray payload("test=test");
                    QNetworkReply *reply = m_nam->post(req, payload);
                    connect(reply,&QNetworkReply::finished,this,[&](){
                      qDebug() << payload;
                      qDebug() << reply->readAll();
                      reply->deleteLater();
                    });
                    
                    J Offline
                    J Offline
                    Jiloc
                    wrote on last edited by
                    #9

                    @sneubert Yep it works, good idea! I will use your suggestion but I will leave the question unsolved for now.
                    I am a bit surprised that there are no "standard" ways to do it, or at least that (it seems like) there is no direct access to that payload from the reply!

                    JKSHJ 1 Reply Last reply
                    0
                    • J Jiloc

                      Hello everyone,

                      does someone know how to access the data that I sent with a post or put request from the reply?

                      for example when the finished signal is fired and the connected slot is called how can I access the data that I originally sent?

                      Whathever::Whathever() 
                      { 
                          m_nam = new QNetworkAccessManager;
                          connect(m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
                      }
                      
                      void Whathever::testPost()
                      {
                          QNetworkRequest req("http://whatever.com");
                          QByteArray payload("test=test");
                          m_nam->post(req, payload);
                      }
                      
                      void Whathever::onFinished(QNetworkReply *reply)
                      {
                          // Here i want to access the data contained in "payload"
                      }
                      
                      C Offline
                      C Offline
                      ctestos
                      wrote on last edited by ctestos
                      #10

                      @Jiloc

                      I solved the problem by creating my own QNetworkAccessManager in order to catch the post data in a variable and emit them with a signal:

                      #ifndef CLSNAMTEST_H
                      #define CLSNAMTEST_H

                      #include <QNetworkAccessManager>
                      #include <QNetworkReply>

                      class clsNamTest : public QNetworkAccessManager
                      {
                      Q_OBJECT
                      public:
                      clsNamTest(QObject * parent = 0)
                      : QNetworkAccessManager() {}

                      virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& request, QIODevice *outgoingData =0)
                      {
                          QString strUrl = request.url().toString();
                      
                          if (strUrl.indexOf("/json?h") != -1)
                          {
                              if (outgoingData !=0)
                              {
                                  QByteArray baTest;
                                  QString strTest= request.rawHeader(QString("Content-Length").toUtf8());
                                  baTest = outgoingData->peek(strTest.toInt());
                                  emit dataGot(baTest);
                                  QString str(baTest);
                                  qDebug() << str;
                              }
                          }
                          reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
                          return reply;
                      }
                      

                      signals:
                      void dataGot(QByteArray data);

                      private:
                      QNetworkReply* reply;
                      };

                      #endif // CLSNAMTEST_H

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        What is wrong with QNetworkReply::request() ?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        JonBJ C 2 Replies Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          What is wrong with QNetworkReply::request() ?

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

                          @Christian-Ehrlicher
                          You have to read earlier post from OP:

                          Jiloc 28 Sep 2017, 15:50

                          @jsulm As I wrote 2 posts above ,I already did. The QNetworkRequest doesn't contains the PAYLOAD of the request. In fact when you send a post or put request you set 2 different parameters in the QNetworkAccessManager methods.

                          http://doc.qt.io/qt-5/qnetworkaccessmanager.html#post-1

                          Whether that's true or not I don't know, but a couple of people seem to think it is....

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            What is wrong with QNetworkReply::request() ?

                            C Offline
                            C Offline
                            ctestos
                            wrote on last edited by ctestos
                            #13

                            @Christian-Ehrlicher
                            The problem is, that the QNetworkAccessManager is associated with a QWebView. If the QWebView has received the reply is the QByteArray empty. So I can't read it again with readall() again.

                            I must call the request again, but therefor I need the posted Json Data from the request before, which was automatic generated and called from the QWebView when I log into a browsergame.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Still don't understand why you can't get the request from the reply where you get empty data from...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              J 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                Still don't understand why you can't get the request from the reply where you get empty data from...

                                J Offline
                                J Offline
                                Jiloc
                                wrote on last edited by
                                #15

                                @Christian-Ehrlicher
                                When you make a post or put request you have to pass 2 parameters to the functions, the QNetworkRequest containing the header and the QByteArray containing the payload.

                                From the response you can access to the associated QNetworkRequest, but not to the QByteArray.

                                Hope it is clearer now!

                                1 Reply Last reply
                                0
                                • J Jiloc

                                  @sneubert Yep it works, good idea! I will use your suggestion but I will leave the question unsolved for now.
                                  I am a bit surprised that there are no "standard" ways to do it, or at least that (it seems like) there is no direct access to that payload from the reply!

                                  JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @Jiloc said in Access data sent with post/put from QNetworkReply:

                                  I am a bit surprised that there are no "standard" ways to do it, or at least that (it seems like) there is no direct access to that payload from the reply!

                                  The payload could be quite large, so it doesn't make sense for the reply to contain a copy of the request payload.

                                  If you want a copy, you should just make the copy before posting your request. (From your latest posts, it sounds like you're already doing this)

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  1 Reply Last reply
                                  3
                                  • V Offline
                                    V Offline
                                    vlef
                                    wrote on last edited by
                                    #17

                                    @Jiloc what you said is pretty clear... I think no one read you anwser or try to understand you problem.

                                    I do because I'm in the same case than you : I want to access of the request's payload that is not availaible in the QNetworkRequest.
                                    For people : readAll on QNetworkReply read the payload of the reply not the request.

                                    Unfortunately no way to get the request payload from the reply or reply->request() object => It's up to you to keep it when sending it and associate this payload to the reply.

                                    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 Extensions
                                    • Unsolved