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. QNetworkAccessManager post() not sending body
QtWS25 Last Chance

QNetworkAccessManager post() not sending body

Scheduled Pinned Locked Moved Unsolved General and Desktop
networkaccessmaqt5.6linux desktop
5 Posts 3 Posters 972 Views
  • 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.
  • C Offline
    C Offline
    candy76041820
    wrote on 4 Jan 2023, 02:40 last edited by
    #1

    Yeah I found this post here: QNetworkAccessManager is not sending data part of POST request, but it doesn't apply to my case.

    QNetorkAccessManager nam;//static member of a class - shouldn't go out of scope at all

    QByteArray json="{...}";//Generated every time
    QNetworkRequest req(QUrl("http://authority/..."));//URL rotates periodically with requests
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QNetworkReply *rep=nam.post(req, json);
    //connect()ing signals

    On Windows Qt5.12, everything looks fine. But on Linux Qt5.6.1, after 9 requests, server logs and Wireshark tell me that nam isn't sending the json body.
    Tried copying the post body QByteArray to a new-ed QBuffer (lest it gets destructed), but that didn't change anything.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 4 Jan 2023, 06:09 last edited by
      #2

      QNetorkAccessManager nam;//static member of a class - shouldn't go out of scope at all

      Static? Why?

      But on Linux Qt5.6.1,

      Such an old version. Reason?

      The rest of your post provides nothing that can be used to reproduce your problem. You are claiming that code like this does not work:

      #include <QCoreApplication>
      #include <QDebug>
      #include <QObject>
      #include <QNetworkAccessManager>
      #include <QNetworkReply>
      #include <QByteArray>
      
      class Sender: public QObject {
          Q_OBJECT
      public:
          explicit Sender(QObject *p = nullptr): QObject(p) {
              m_nam = new QNetworkAccessManager(this);
              m_rep = nullptr;
          }
          ~Sender() {}
      
          void send() {
              QByteArray json(R"( {"title": "foo", "body": "bar", "userId": "45678" } )");
              QNetworkRequest req(QUrl("http://jsonplaceholder.typicode.com/posts"));
              req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
              m_rep = m_nam->post(req, json);
              connect(m_rep, &QNetworkReply::finished, this, &Sender::handleFinished);
              connect(m_rep, &QNetworkReply::errorOccurred, this, &Sender::handleError);
          }
      
      private slots:
          void handleFinished() {
              qDebug() << Q_FUNC_INFO << m_rep->readAll();
          }
          void handleError(QNetworkReply::NetworkError code) {
              qDebug() << Q_FUNC_INFO << code;
          }
      
      private:
          QNetworkAccessManager *m_nam;
          QNetworkReply *m_rep;
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          Sender sender;
          sender.send();
      
          return a.exec();
      }
      #include "main.moc"
      

      The POST data quite happily goes out and a response comes back for me (Qt 5.15 on Linux).
      How about with your ancient version of Qt?

      C 1 Reply Last reply 12 Jan 2023, 02:37
      2
      • C ChrisW67
        4 Jan 2023, 06:09

        QNetorkAccessManager nam;//static member of a class - shouldn't go out of scope at all

        Static? Why?

        But on Linux Qt5.6.1,

        Such an old version. Reason?

        The rest of your post provides nothing that can be used to reproduce your problem. You are claiming that code like this does not work:

        #include <QCoreApplication>
        #include <QDebug>
        #include <QObject>
        #include <QNetworkAccessManager>
        #include <QNetworkReply>
        #include <QByteArray>
        
        class Sender: public QObject {
            Q_OBJECT
        public:
            explicit Sender(QObject *p = nullptr): QObject(p) {
                m_nam = new QNetworkAccessManager(this);
                m_rep = nullptr;
            }
            ~Sender() {}
        
            void send() {
                QByteArray json(R"( {"title": "foo", "body": "bar", "userId": "45678" } )");
                QNetworkRequest req(QUrl("http://jsonplaceholder.typicode.com/posts"));
                req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
                m_rep = m_nam->post(req, json);
                connect(m_rep, &QNetworkReply::finished, this, &Sender::handleFinished);
                connect(m_rep, &QNetworkReply::errorOccurred, this, &Sender::handleError);
            }
        
        private slots:
            void handleFinished() {
                qDebug() << Q_FUNC_INFO << m_rep->readAll();
            }
            void handleError(QNetworkReply::NetworkError code) {
                qDebug() << Q_FUNC_INFO << code;
            }
        
        private:
            QNetworkAccessManager *m_nam;
            QNetworkReply *m_rep;
        };
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            Sender sender;
            sender.send();
        
            return a.exec();
        }
        #include "main.moc"
        

        The POST data quite happily goes out and a response comes back for me (Qt 5.15 on Linux).
        How about with your ancient version of Qt?

        C Offline
        C Offline
        candy76041820
        wrote on 12 Jan 2023, 02:37 last edited by
        #3

        @ChrisW67
        static: to ensure it doesn't destruct.
        5.6.1: it's the preinstalled version for my distro.
        Scenario is I'm inventing some auto-web-requestor. On the first request I post to one URL to fetch some data, then in the finished() signal I QTimer::singleShot(0, &PostNextRequest) with prcocessed result of the previous reply. All the request URLs are on one same server, with paths diffe. For request queues longer than 9, the 9th post body doesn't show on the wire, and the server complains about not receiving request body. Also, QNetworkReply::error(NetworkError) doesn't fire on the abortion of 9th request.
        The exact same code on Windows Qt5.12 works w/out any problem, so I'm guessing it a bug in my distro's Qt.

        J 1 Reply Last reply 12 Jan 2023, 12:12
        0
        • C candy76041820
          12 Jan 2023, 02:37

          @ChrisW67
          static: to ensure it doesn't destruct.
          5.6.1: it's the preinstalled version for my distro.
          Scenario is I'm inventing some auto-web-requestor. On the first request I post to one URL to fetch some data, then in the finished() signal I QTimer::singleShot(0, &PostNextRequest) with prcocessed result of the previous reply. All the request URLs are on one same server, with paths diffe. For request queues longer than 9, the 9th post body doesn't show on the wire, and the server complains about not receiving request body. Also, QNetworkReply::error(NetworkError) doesn't fire on the abortion of 9th request.
          The exact same code on Windows Qt5.12 works w/out any problem, so I'm guessing it a bug in my distro's Qt.

          J Offline
          J Offline
          JonB
          wrote on 12 Jan 2023, 12:12 last edited by
          #4

          @candy76041820 said in QNetworkAccessManager post() not sending body:

          5.6.1: it's the preinstalled version for my distro.

          Qt 5.6 was released in 2016. What distro are you currently using which supplies/expects you to use that?

          If you are using 5.12 elsewhere you may be sorely disappointed at 5.6 bugs or missing features. Everybody here would advise you to move on from 5.6.

          C 1 Reply Last reply 14 Jan 2023, 02:16
          0
          • J JonB
            12 Jan 2023, 12:12

            @candy76041820 said in QNetworkAccessManager post() not sending body:

            5.6.1: it's the preinstalled version for my distro.

            Qt 5.6 was released in 2016. What distro are you currently using which supplies/expects you to use that?

            If you are using 5.12 elsewhere you may be sorely disappointed at 5.6 bugs or missing features. Everybody here would advise you to move on from 5.6.

            C Offline
            C Offline
            candy76041820
            wrote on 14 Jan 2023, 02:16 last edited by candy76041820
            #5

            @JonB It's an internal (private) distro, based on som ancient version of Ubuntu, I guess. Preinstalled on all of thier machines, so I have to stick to that.
            (Hell they even have Qt4.8 preinstalled sxs!)

            1 Reply Last reply
            1

            • Login

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