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. Problem with losting body when I send using QNetworkRequest with method POST
Forum Updated to NodeBB v4.3 + New Features

Problem with losting body when I send using QNetworkRequest with method POST

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 712 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.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by
    #1

    I wrote my http server and client. I send HTTP request using QNetworkRequest on my client and then get data on server and output it. But in most cases body is plane, however content-legnth is present. Also, I can't figure out why http request displaying twice time when I do only one request from my server.

    My client

        const QUrl url = QUrl("http://localhost:8080/test/123");
        QNetworkRequest request(url);
        QNetworkReply *reply = manager->post(request, "SOME TEXT IN MY BODY");
    

    Server

    void HttpServer::incomingConnection(qintptr socketDescriptor)
    {
        Runnable *task = new Runnable();
        task->setAutoDelete(true);
    
        task->socketDescriptor = socketDescriptor;
        task->storage = storage;
    
        pool->start(task);
    }
    
    
    void Runnable::run()
    {
        if(!socketDescriptor) return;
    
        QTcpSocket socket;
        socket.setSocketDescriptor(socketDescriptor);
    
        if(socket.waitForReadyRead())
        {
            qDebug() << socket.readAll();
        }
    
        socket.close();
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      Subuday
      wrote on last edited by Subuday
      #2

      Setting Content TypeHeader doesn't help.

      request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
      

      But the ploblem with twice outputing is still unsolver. Moreover, if I send request from GOOGLE it repeats 6 times.

      jsulmJ 1 Reply Last reply
      0
      • S Subuday

        Setting Content TypeHeader doesn't help.

        request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
        

        But the ploblem with twice outputing is still unsolver. Moreover, if I send request from GOOGLE it repeats 6 times.

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

        @Subuday said in Problem with losting body when I send using QNetworkRequest with method POST:

        But the ploblem with twice outputing is still unsolver

        Not sure what exact output you mean, but it could be due to TCP/IP splitting your data into more than one packet when sending it.

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

        S 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Subuday said in Problem with losting body when I send using QNetworkRequest with method POST:

          But the ploblem with twice outputing is still unsolver

          Not sure what exact output you mean, but it could be due to TCP/IP splitting your data into more than one packet when sending it.

          S Offline
          S Offline
          Subuday
          wrote on last edited by Subuday
          #4

          @jsulm !!Setting Content Type doesn't help.!!! The body still is been losting. Example of problem: (On screenshot body is lost)
          =0_1557393034390_Screenshot from 2019-05-09 12-10-14.png

          jsulmJ 1 Reply Last reply
          0
          • S Subuday

            @jsulm !!Setting Content Type doesn't help.!!! The body still is been losting. Example of problem: (On screenshot body is lost)
            =0_1557393034390_Screenshot from 2019-05-09 12-10-14.png

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

            @Subuday Did you try to debug to see what happens?

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

            S 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Subuday Did you try to debug to see what happens?

              S Offline
              S Offline
              Subuday
              wrote on last edited by Subuday
              #6

              @jsulm Yes, I check that client make request only one time. void HttpServer::incomingConnection(qintptr socketDescriptor) is called twice times. Sometimes even three or four times.

              jsulmJ 1 Reply Last reply
              0
              • S Subuday

                @jsulm Yes, I check that client make request only one time. void HttpServer::incomingConnection(qintptr socketDescriptor) is called twice times. Sometimes even three or four times.

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

                @Subuday Where is incomingConnection called? That is what I actually wanted you to debug (or check your code).

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

                S 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Subuday Where is incomingConnection called? That is what I actually wanted you to debug (or check your code).

                  S Offline
                  S Offline
                  Subuday
                  wrote on last edited by
                  #8

                  @jsulm When some connection happened incomingConnection called.

                  int main(int argc, char *argv[])
                  {
                      QCoreApplication a(argc, argv);
                  
                      Storage storage("/home/max/Desktop/chokiDB");
                  
                      HttpServer server;
                      server.startServer();
                  
                      return a.exec();
                  }
                  
                  
                  
                  HttpServer::HttpServer(QObject *parent) : QTcpServer(parent)
                  {
                      pool = new QThreadPool(this);
                  }
                  
                  void HttpServer::startServer()
                  {
                      if(listen(QHostAddress::Any, 1234))
                      {
                          qDebug() << "Server started";
                      }
                      else
                      {
                          qDebug() << "Server: not started";
                      }
                  
                  }
                  
                  void HttpServer::incomingConnection(qintptr socketDescriptor)
                  {
                      Runnable *task = new Runnable();
                      task->setAutoDelete(true);
                  
                      task->socketDescriptor = socketDescriptor;
                      task->storage = storage;
                  
                      pool->start(task);
                  }
                  
                  void Runnable::run()
                  {
                      if(!socketDescriptor) return;
                  
                      QTcpSocket socket;
                      socket.setSocketDescriptor(socketDescriptor);
                  
                      if(socket.waitForReadyRead())
                      {
                          parse(socket.readAll());
                      }
                  
                      socket.close();
                  }
                  
                  int main(int argc, char *argv[])
                  {
                      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                  
                      QGuiApplication app(argc, argv);
                  
                      Client client;
                      client.test();
                  
                      QQuickStyle::setStyle("Material");
                  
                      QScopedPointer<MyImageProvider> myImageProvider(new MyImageProvider());
                  
                      QQmlApplicationEngine engine;
                      const QUrl url(QStringLiteral("qrc:/qml/StartWindow.qml"));
                  
                      Storage storage("/home/max/Desktop/chokiDB");
                  
                      myImageProvider.data()->setStorage(&storage);
                  
                      FilterProxyModel filterModel;
                      storage.setFilterModel(&filterModel);
                  
                      QQmlContext *context = engine.rootContext();
                      context->setContextProperty("storage",&storage);
                      context->setContextProperty("filterModel", &filterModel);
                      context->setContextProperty("myImageProvider", myImageProvider.data());
                      engine.addImageProvider(QLatin1String("avatar"), myImageProvider.data());
                      engine.load(url);
                  
                      return app.exec();
                  }
                  
                  void Client::test()
                  {
                      const QUrl url = QUrl("http://localhost:1234/test/123");
                      QNetworkRequest request(url);
                      request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
                      QNetworkReply *reply = manager->post(request, "SOME TEXT IN MY BODY 123");
                  }
                  
                  
                  
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • S Subuday

                    @jsulm When some connection happened incomingConnection called.

                    int main(int argc, char *argv[])
                    {
                        QCoreApplication a(argc, argv);
                    
                        Storage storage("/home/max/Desktop/chokiDB");
                    
                        HttpServer server;
                        server.startServer();
                    
                        return a.exec();
                    }
                    
                    
                    
                    HttpServer::HttpServer(QObject *parent) : QTcpServer(parent)
                    {
                        pool = new QThreadPool(this);
                    }
                    
                    void HttpServer::startServer()
                    {
                        if(listen(QHostAddress::Any, 1234))
                        {
                            qDebug() << "Server started";
                        }
                        else
                        {
                            qDebug() << "Server: not started";
                        }
                    
                    }
                    
                    void HttpServer::incomingConnection(qintptr socketDescriptor)
                    {
                        Runnable *task = new Runnable();
                        task->setAutoDelete(true);
                    
                        task->socketDescriptor = socketDescriptor;
                        task->storage = storage;
                    
                        pool->start(task);
                    }
                    
                    void Runnable::run()
                    {
                        if(!socketDescriptor) return;
                    
                        QTcpSocket socket;
                        socket.setSocketDescriptor(socketDescriptor);
                    
                        if(socket.waitForReadyRead())
                        {
                            parse(socket.readAll());
                        }
                    
                        socket.close();
                    }
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                    
                        QGuiApplication app(argc, argv);
                    
                        Client client;
                        client.test();
                    
                        QQuickStyle::setStyle("Material");
                    
                        QScopedPointer<MyImageProvider> myImageProvider(new MyImageProvider());
                    
                        QQmlApplicationEngine engine;
                        const QUrl url(QStringLiteral("qrc:/qml/StartWindow.qml"));
                    
                        Storage storage("/home/max/Desktop/chokiDB");
                    
                        myImageProvider.data()->setStorage(&storage);
                    
                        FilterProxyModel filterModel;
                        storage.setFilterModel(&filterModel);
                    
                        QQmlContext *context = engine.rootContext();
                        context->setContextProperty("storage",&storage);
                        context->setContextProperty("filterModel", &filterModel);
                        context->setContextProperty("myImageProvider", myImageProvider.data());
                        engine.addImageProvider(QLatin1String("avatar"), myImageProvider.data());
                        engine.load(url);
                    
                        return app.exec();
                    }
                    
                    void Client::test()
                    {
                        const QUrl url = QUrl("http://localhost:1234/test/123");
                        QNetworkRequest request(url);
                        request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
                        QNetworkReply *reply = manager->post(request, "SOME TEXT IN MY BODY 123");
                    }
                    
                    
                    
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Subuday said in Problem with losting body when I send using QNetworkRequest with method POST:

                    When some connection happened incomingConnection called

                    This is clear, but where exactly is it called?
                    Also, what is HttpServer? What is it's base class?

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

                    1 Reply Last reply
                    2

                    • Login

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