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. Cannot receive HTTP multipart request completely with QTcpServer.

Cannot receive HTTP multipart request completely with QTcpServer.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 689 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.
  • F Offline
    F Offline
    FloatFlower.Huang
    wrote on last edited by FloatFlower.Huang
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      FloatFlower.Huang
      wrote on last edited by
      #2

      That my fault.
      I remove the function call:

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

      and it works.

      1 Reply Last reply
      0

      • Login

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