Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QNetworkAccessManager is not sending data part of POST request

    General and Desktop
    qnetworkaccessm http-post
    3
    4
    2884
    Loading More Posts
    • 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.
    • CupaMurdosan
      CupaMurdosan last edited by CupaMurdosan

      When sending POST data to server, from Qt application looks everything good but data part of HTTP part were not sent. In Wireshark in POST packet is visible correct "Content-Length" value but size of whole HTTP segment is only about 226 bytes (is independent on POST data size).

      Here is simplified source code:

      project.pro:

      QT += widgets
      QT -= gui
      QT += network
      
      CONFIG += c++11
      
      TARGET = POSTrequest
      CONFIG += console
      CONFIG -= app_bundle
      
      TEMPLATE = app
      
      SOURCES += main.cpp
      

      main.cpp:

      #include <QObject>
      #include <QApplication>
      #include <QNetworkAccessManager>
      #include <QNetworkRequest>
      #include <QUrl>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QNetworkAccessManager manager;
          QByteArray array = "a=aaaaaaaaaaaaaa..."; // content length > 500
          QNetworkRequest r( QUrl("http://www.server.com/index.php") );
          r.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
      
          QEventLoop loop;
          QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &loop, SLOT(quit()));
          
          manager.post(r, array);
      
          loop.exec();
      
          return a.exec();
      }
      

      I can't find reason why data part is not send. What I doing wrong? Or, its bug?
      Application is running in console.
      Using Qt 5.6.0.

      1 Reply Last reply Reply Quote 0
      • P
        Paul Colby last edited by

        Hi @CupaMurdosan,

        Why are you instantiating your own QEventLoop?

        What happens if you leave out the QEventLoop entirely (I don't think it should be necessary). Instead of connecting to the loop's quit slot, you should be able to connect to the application's quit slot directly, eg:

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            ...
        
            //QEventLoop loop;
            //QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &loop, SLOT(quit()));
            QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &a, SLOT(quit()));
            
            manager.post(r, array);
        
            //loop.exec();
        
            return a.exec();
        }
        

        Give that a go :)

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi and welcome to devnet,

          To add to @paul-colby, you should also connect the error signal to see whether there's something wrong going on.

          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 Reply Quote 0
          • CupaMurdosan
            CupaMurdosan last edited by

            Thank you guys for your replies. Problem is solved - I was wrong with wireshark - data were sent in another packet [TCP segment of a reassembled PDU].

            1 Reply Last reply Reply Quote 0
            • First post
              Last post