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. c++ Rest APi 3rd party integration Question

c++ Rest APi 3rd party integration Question

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 542 Views
  • 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    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

    Christian EhrlicherC 1 Reply Last reply
    0
    • R RahibeMeryem

      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

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by RahibeMeryem
        #3

        @Christian-Ehrlicher bool connected and connected_ is true .

        what am I missing ?

        or how can I connect ?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RahibeMeryem
            wrote on last edited by
            #5

            @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" });
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • R Offline
                R Offline
                RahibeMeryem
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    RahibeMeryem
                    wrote on last edited by
                    #9

                    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

                    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