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



  • 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();
    }
    


  • 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.


  • Qt Champions 2018

    @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.



  • @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


  • Qt Champions 2018

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



  • @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.


  • Qt Champions 2018

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



  • @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");
    }
    
    
    
    

  • Qt Champions 2018

    @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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.