How to transfer a C++ object to a web service using POCO library

霸气de小男生 提交于 2019-12-19 09:55:09

问题


I have an image processing application that uses Qt and openCV.

for each frame, I should send the captured cv::Mat image object to the server to process it and get the results.

I should use the REST architecture for its low playload.

What is the tool that I should use to send cv::Mat to the server.

I am using POCO for portability.

I seek for the lightest solution to do that, I need a minimum speed of 10 frames processed by the server in a second.

I mean, is there a method to pass the C++ Object to the server without an explicit serialization?


回答1:


EDIT

With the POCO library, you can take a look in this answer: HttpRequest PUT content in poco library. He is sending a file on a ifstream.

In this answer you can check how to convert a cv::Mat into a istream: OpenCV cv::Mat to std::ifstream for base64 encoding.

And finally, Thanks to polymorphism, the istream is implicity converted to a ifstream.


You can use the C++ Rest SDK. A code example of the PUT command.

Source of code

Library Github where you can find the full documentation.

#include <http_client.h>
#include <filestream.h>
#include <iostream>
#include <sstream>

using namespace web::http;
using namespace web::http::client;

// Upload a file to an HTTP server.
pplx::task<void> UploadFileToHttpServerAsync()
{
    using concurrency::streams::file_stream;
    using concurrency::streams::basic_istream;

    // To run this example, you must have a file named myfile.txt in the current folder. 
    // Alternatively, you can use the following code to create a stream from a text string. 
    // std::string s("abcdefg");
    // auto ss = concurrency::streams::stringstream::open_istream(s); 

    // Open stream to file. 
    return file_stream<unsigned char>::open_istream(L"myfile.txt").then([](pplx::task<basic_istream<unsigned char>> previousTask)
    {
        try
        {
            auto fileStream = previousTask.get();

            // Make HTTP request with the file stream as the body.
            http_client client(L"http://www.fourthcoffee.com");
            return client.request(methods::PUT, L"myfile", fileStream).then([fileStream](pplx::task<http_response> previousTask)
            {
                fileStream.close();

                std::wostringstream ss;
                try
                {
                    auto response = previousTask.get();
                    ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;
                }
                catch (const http_exception& e)
                {
                    ss << e.what() << std::endl;
                }
                std::wcout << ss.str();
            });
        }
        catch (const std::system_error& e)
        {
            std::wostringstream ss;
            ss << e.what() << std::endl;
            std::wcout << ss.str();

            // Return an empty task. 
            return pplx::task_from_result();
        }
    });

    /* Sample output:
    The request must be resent
    */
}


来源:https://stackoverflow.com/questions/36771435/how-to-transfer-a-c-object-to-a-web-service-using-poco-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!