首页 > 解决方案 > 如何转换 boost::beast 的响应回应?

问题描述

我需要转换boost::beast::http::response<boost::beast::http::buffer_body>boost::beast::http::response<boost::beast::http::string_body>.

使用野兽的 api 这样做的优雅而有效的方法是什么?

附言

我认为序列化和解析不是那么有效,也许有更好的方法。但如果这是解决方案,因为我是野兽新手,我也很高兴看到一个优雅的代码示例。

谢谢,大卫。

标签: c++httpboostboost-beast

解决方案


好的,我设法做到了

boost::beast::http::response<boost::beast::http::string_body> string_response;
boost::beast::http::response<boost::beast::http::buffer_body> buffer_response;
std::string response_body

// Do stuff to read the response and fill the response_body using the buffer

string_response.base() = buffer_response.base();
string_response.body() = response_body;

事实证明,标头有一个复制构造函数,所以我所要做的就是分配字符串主体..

因此,如果复制构造函数是有效的(很可能是这样),那么这个解决方案也是有效的。


推荐阅读