Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to receive file over QHttpEngine::Server



  • Hi,
    I am developing a module, in which i have to recieve the file over HTTP from client.

    I used Swagger and openAPI generator to create classes and cmake file. Below is the section where i have to handle the file. This module is a server and it has to recieve/download the file from client

    How to read/download the file ?

    PS: Using postman to upload the file (as form-data).

    void OAIPkgupdaterApiRequest::pkgupdaterUpdatePostRequest(){
        qDebug() << "/v1/pkgupdater/update";
        connect(this, &OAIPkgupdaterApiRequest::pkgupdaterUpdatePost, handler.data(), &OAIPkgupdaterApiHandler::pkgupdaterUpdatePost);
    
        OAIHttpFileElement uploadfile;
        QString imagename;
        QString key;
        QString overwrite;
    
        emit pkgupdaterUpdatePost(uploadfile, imagename, key, overwrite);
    }
    
    

    Below snippet from Main function

        // Obtain the values
        QHostAddress address = QHostAddress(parser.value(addressOption));
        quint16 port = static_cast<quint16>(parser.value(portOption).toInt());
    
        QSharedPointer<OpenAPI::OAIApiRequestHandler> handler(new OpenAPI::OAIApiRequestHandler());
        auto router = QSharedPointer<OpenAPI::OAIApiRouter>::create();
        router->setUpRoutes();
        QObject::connect(handler.data(), &OpenAPI::OAIApiRequestHandler::requestReceived, [&](QHttpEngine::Socket *socket) {
            router->processRequest(socket);
        });
    
        QHttpEngine::Server server(handler.data());
        qDebug() << "Serving on " << address.toString() << ":" << port;
        // Attempt to listen on the specified port
        if (!server.listen(address, port)) {
            qCritical("Unable to listen on the specified port.");
            return 1;
        }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you receive all the data in one go ?
    If so, I would say use QFile to write the content of the file to disk.



  • @SGaist Thanks for your reply.

    If i pass JSON objects in the postman Body i could see the attributes with the below set of codes. I am aware how to read the file or download the file in the server end. I belive QFile is to handle the local file, i am looking for the file handling over http

    QJsonDocument doc 
    socket->readJson(doc);
    QJsonObject obj = doc.object();
    
    

  • Lifetime Qt Champion

    Well, server side, you will have to save your file at some point. That's when QFile comes handy.

    Just in case, you might want to check the Cutelyst project.


Log in to reply