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

c++ Rest APi 3rd party integration Question



  • Hi,

    I try to use webcc rest server with Qt framework.

    Its working so far so good.

    I want to get form data via signal slot but sitll can not.

    resapi.h

    #ifndef RESTAPI_H
    #define RESTAPI_H
    
    //#include "rest_server.h"
    #include <fstream>
    #include <iostream>
    #include <string>
    
    #include "webcc/logger.h"
    #include "webcc/response_builder.h"
    #include "webcc/server.h"
    #include "opencv2/core.hpp"
    #include <opencv2/imgcodecs.hpp>
    #include <QPixmap>
    
    #include <QObject>
    
    class RestApi : public QObject , public webcc::View
    {
        Q_OBJECT
    
    public:
        explicit RestApi (QObject *parent = nullptr) ;
        int image_counter = 0;
    
    public slots:
    
        void start_Rest();
        webcc::ResponsePtr Handle(webcc::RequestPtr request) ;
    
        webcc::ResponsePtr Post(webcc::RequestPtr request);
        void sendPix(QPixmap &pix);
    
    signals:
        void rest_image_received(const QPixmap &qpix);
        void test_signal();
    
    };
    
    #endif // RESTAPI_H
    
    

    restapi.cc

    #include "restapi.h"
    #include <QDebug>
    RestApi::RestApi(QObject *parent) : QObject(parent)
    {
        qDebug() << "xx restapi.cc  REST API started " << endl;
    
    }
    
    
    void RestApi::start_Rest()
    {
    
        int PORT = 7000;
        WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
    
        std::uint16_t port = static_cast<std::uint16_t>(PORT);
    
        try {
            webcc::Server server{ boost::asio::ip::tcp::v4(), port };
    
            server.set_buffer_size(webcc::kBufferSize * 10);
    
            server.Route("/upload", std::make_shared<RestApi>(), { "POST" });
    
            server.Run();
    
        } catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
    //        return 1;
        }
    }
    
    webcc::ResponsePtr RestApi::Handle(webcc::RequestPtr request)
    {
    
        if (request->method() == "POST") {
            return Post(request);
        }
    
        return {};
    }
    
    webcc::ResponsePtr RestApi::Post(webcc::RequestPtr request)
    {
        emit test_signal ();
        std::cout << "form parts: " << request->form_parts().size() << std::endl;
        std::string resp ;
        for (auto& part : request->form_parts()) {
            std::cout << "name: " << part->name() << std::endl;
            resp = part->name();
            if (part->file_name().empty()) {
                std::cout << "data: " << part->data() << std::endl;
            } else {
                size_t size=part->data().size();
    
                if(true){
                    std::ofstream myFile ("/Users/tulpar/Projects/webcc-21Dec/build/webccData-QT"+ std::to_string (image_counter++) +".jpeg", std::ios::out | std::ios::binary);
                    myFile << part->data();
    
                    myFile.close ();
                    QByteArray byteArray(part->data().c_str(), part->data().length());
    
                    QPixmap img;
                    img.loadFromData (byteArray);
                    RestApi::sendPix (img);
                }
    
                // Save part->data() as binary to file.
                // ...
            }
        }
    
        return webcc::ResponseBuilder{}.Created().Body(" ..  OK " + resp)();
    }
    
    void RestApi::sendPix(QPixmap &pix)
    {
        emit rest_image_received (pix);
    }
    
    
    
    

    mainwindow.cc
    ......

    bool connected = connect (restApi ,  &RestApi::rest_image_received , this , &MainWindow::showRestImage);
    
        bool connected_ = connect (restApi ,  &RestApi::test_signal , this , &MainWindow::test_sig);
        
    

    its not receiving the pix

    Best


  • Lifetime Qt Champion

    @RahibeMeryem said in c++ Rest APi 3rd party integration Question:

    server.Route("/upload", std::make_shared<RestApi>(), { "POST" });

    This creates a new instance where the signal is not connected.



  • @Christian-Ehrlicher bool connected and connected_ is true .

    what am I missing ?

    or how can I connect ?


  • Lifetime Qt Champion

    @RahibeMeryem said in c++ Rest APi 3rd party integration Question:

    what am I missing ?

    You did not read my post. You're creating a new instance where you don't connect anything to it.



  • @RahibeMeryem said in c++ Rest APi 3rd party integration Question:

        server.Route("/upload", std::make_shared<RestApi>(), { "POST" });
    

    @Christian-Ehrlicher is there anyway to connect :

        server.Route("/upload", std::make_shared<RestApi>(), { "POST" });

  • Lifetime Qt Champion

    @RahibeMeryem said in c++ Rest APi 3rd party integration Question:

    is there anyway to connect :

    Yes, why not. You can create the needed pointer earlier.



  • @Christian-Ehrlicher

    Thank you for your help.

    I am a user of c++ mostly. not deep knowledge.

    Would you mind to show me where can I put ?

    Best


  • Lifetime Qt Champion

    @RahibeMeryem said in c++ Rest APi 3rd party integration Question:

    Would you mind to show me where can I put ?

    It's basic stuff. Don't think it will help you in any way when you simply copy it from somewhere else.
    I would suggest to pass the shared pointer to the start_Rest() function and therefore you can do the connection from where you call this function.



  • Hi @Christian-Ehrlicher

    https://github.com/sprinfall/webcc/tree/master/examples/qt_app_server

    is came up a good example which I use it . Thanks to Chunting Gu aka: sprinfall
    
    

    Thank you.

    Best


Log in to reply