Cannot receive HTTP multipart request completely with QTcpServer.



  • I am going to write a HTTP server with QTcpServer, however, when I POST a form, which consist of 2 images:

    #include <QDebug>
    #include <QTcpSocket>
    #include "server.h"
    
    Server::Server(QObject *parent) : QObject(parent)
    {
        m_server = new QTcpServer(this);
        connect(m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
    }
    
    void Server::start()
    {
        m_server->listen(QHostAddress::LocalHost, 3000);
    }
    
    void Server::onNewConnection()
    {
        qDebug() << "New Connection established!";
        if (m_server->hasPendingConnections())
        {
            QTcpSocket* socket = m_server->nextPendingConnection();
            connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
        }
    }
    
    void Server::onReadyRead()
    {
        QObject *object = sender();
        QTcpSocket* socket = qobject_cast<QTcpSocket*>(object);
        qDebug() << socket->readAll().constData();
    }
    

    And I get the log:

    New Connection established!
    POST / HTTP/1.1
    Host: localhost:3000
    Connection: keep-alive
    Content-Length: 72213
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
    Cache-Control: no-cache
    Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
    Postman-Token: 3b55da60-6aa1-dbb3-6aab-b3203511fe3f
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary4TVn7aSuuFAV4SrU
    Accept: */*
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7,zh-CN;q=0.6,ja;q=0.5
    
    ------WebKitFormBoundary4TVn7aSuuFAV4SrU
    Content-Disposition: form-data; name="asda"; filename="whatever.png"
    Content-Type: image/png
    
    ?PNG
    
    
    ??xkUi????[`?W???      HLH@bb"??<1??:u
    

    however, a HTTP request which consist of two images should be in the form:

    POST / HTTP/1.1
    Host: localhost:3000
    Cache-Control: no-cache
    Postman-Token: dcae3f50-560a-7d19-a8c3-64afea5b362f
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
    
    ------WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="asda"; filename="whatever.png"
    Content-Type: image/png
    
    // body here
    ------WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="asdaasd"; filename="whatever.png"
    Content-Type: image/png
    
    // body here
    ------WebKitFormBoundary7MA4YWxkTrZu0gW--
    

    The log means that we cannot receive the request payload completely, what's wrong with this code, or is caused by Postman?



  • That my fault.
    I remove the function call:

    // Before
    qDebug() << socket->readAll().constData();
    // After
    qDebug() << socket->readAll();
    

    and it works.


Log in to reply
 

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