Boost.Beast: 是否可以构造HTTP批量请求?

Boost.Beast: Is it possible to construct an HTTP batch request?

我使用 Boost.Beast 发送 HTTP 请求已经有一段时间了,我认为它很棒并且运行良好。但是有谁知道是否可以使用 Beast 构造 HTTP 批处理请求?我正在考虑创建多个子请求:

boost::beast::http::request<boost::beast::http::string_body> subrequest_1;

subrequest_1.set(boost::beast::http::field::content_type, "application/http");

...

boost::beast::http::request<boost::beast::http::string_body> subrequest_2;

subrequest_2.set(boost::beast::http::field::content_type, "application/http");

...

然后以某种方式将它们组合在一起并在一个请求中将它们全部发送。

我一直在尝试创建一个向量,向其添加子请求,然后将向量分配给我要发送的请求的主体,但一直没有成功。

/*

std::vector<boost::beast::http::request<boost::beast::http::string_body>> v;
v.push_back(subrequest_1);

v.push_back(subrequest_2);

boost::beast::http::request<boost::beast::http::string_body> request;

request.method(boost::beast::http::verb::post);
...
request.body() = v;   
*/

提前致谢!

嗯,这个原来很简单。我将 post 我的解决方案放在这里,以防其他人像我几天前一样感到迷茫。

    // Set up the request with a new content type
    boost::beast::http::request<boost::beast::http::string_body> request;
    request.set(field::content_type, "multipart/mixed; boundary='subrequest_boundary'");

    // A string to hold the entire batch request
    std::string body;

    // Add all messages in e.g. a queue to the string
    while ( messages.size() )
    {
        auto message = messages.front();

        // Add a boundary to separate the messages
        body.append("--subrequest_boundary");
        body.append("Content-Type: application/http\n");
        /*
        And all more headers you need
        */

        // I used the nlohmann library to add json docs
        nlohmann::json payload =    {
                                        {"message",
                                        }
                                    };
        body.append( payload.dump() );
        messages.pop();
    }
    // Add a boundary as wrap up
    body.append("--subrequest_boundary--");

    // Add the batch request to the request
    request.body() = body;
    request.prepare_payload();