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

后端 未结 1 1327
夕颜
夕颜 2021-01-14 17:46

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

相关标签:
1条回答
  • 2021-01-14 18:03

    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
        */
    }
    
    0 讨论(0)
提交回复
热议问题