将boost beast http请求转换为字符串并写入ostringstream
我正在尝试使用boost beast进行http调用,并希望在写入套接字之前写入它;我尝试使用ostringstream获取请求的值,以便在打印的日志中获取该值,并获取以下错误消息:将boost beast http请求转换为字符串并写入ostringstream,boost,boost-beast,ostringstream,Boost,Boost Beast,Ostringstream,我正在尝试使用boost beast进行http调用,并希望在写入套接字之前写入它;我尝试使用ostringstream获取请求的值,以便在打印的日志中获取该值,并获取以下错误消息: string verb = "POST"; using http::field; http::request<http::string_body> request; request.method_string(verb); request.target(server_endpoint
string verb = "POST";
using http::field;
http::request<http::string_body> request;
request.method_string(verb);
request.target(server_endpoint);
request.version(11);
request.set(field::host, hostname);
request.set(field::accept, "*/*");
request.set(field::authorization, authorization_token);
request.set(field::user_agent, client_name);
request.set(field::content_length, req_str.length());
request.set(field::content_type, "application/x-www-form-urlencoded");
request.set(field::connection, field::close);
request.body() = req_str;
request.prepare_payload();
fast_ostringstream oss;
oss <<"Request message" << request;
PBLOG_INFO(oss.str());
oss.clear();
HttpTracRequestHandler.cpp:78:26: error: invalid operands to binary expression ('framework::string_::fast_ostringstream' and
'http::request<http::string_body>' (aka 'boost::beast::http::message<true, boost::beast::http::basic_string_body<char,
std::char_traits<char>, std::allocator<char> >, boost::beast::http::basic_fields<std::allocator<char> > >'))
oss <<"Request message" << request;
~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~
/BARXPBViewstore/GIT_Workspace/barx_rel/mq8_barx/barx-server/appl/include/FoxException/GenericException.h:133:19: note: candidate function
[with TData = boost::beast::http::message<true, boost::beast::http::basic_string_body<char, std::char_traits<char>,
std::allocator<char> >, boost::beast::http::basic_fields<std::allocator<char> > >] not viable: no known conversion from
'framework::string_::fast_ostringstream' to 'GenericException &' for 1st argument
GenericException& operator<<(GenericException &e, const TData& data)
^
/BARXPBViewstore/GIT_Workspace/boost/boost_1_70_0/boost/beast/http/impl/write.hpp:921:1: note: candidate function [with isRequest = true,
Body = boost::beast::http::basic_string_body<char, std::char_traits<char>, std::allocator<char> >, Fields =
boost::beast::http::basic_fields<std::allocator<char> >] not viable: no known conversion from 'framework::string_::fast_ostringstream'
to 'std::ostream &' (aka 'basic_ostream<char> &') for 1st argument
operator<<(std::ostream& os,
^
/BARXPBViewstore/GIT_Workspace/boost/boost_1_70_0/boost/beast/http/write.hpp:721:1: note: candidate function [with isRequest = true, Fields
= boost::beast::http::basic_fields<std::allocator<char> >] not viable: no known conversion from
'framework::string_::fast_ostringstream' to 'std::ostream &' (aka 'basic_ostream<char> &') for 1st argument
operator<<(std::ostream& os,
^
/BARXPBViewstore/GIT_Workspace/boost/boost_1_70_0/boost/beast/http/impl/write.hpp:901:1: note: candidate function [with Fields =
boost::beast::http::basic_fields<std::allocator<char> >] not viable: no known conversion from 'framework::string_::fast_ostringstream'
to 'std::ostream &' (aka 'basic_ostream<char> &') for 1st argument
string动词=“POST”;
使用http::field;
http::请求;
请求.方法\字符串(动词);
目标(服务器\终端);
请求.版本(11);
set(字段::主机,主机名);
request.set(字段::accept,“*/*”;
request.set(字段::authorization,authorization\u token);
request.set(字段::user\u agent、client\u name);
set(字段::content_length,req_str.length());
request.set(字段::content_type,“application/x-www-form-urlencoded”);
set(字段::连接,字段::关闭);
request.body()=req_str;
请求。准备_有效载荷();
fast_ostringstream oss;
oss您的代码看起来正常。事实上,它适用于标准字符串流:
#include <boost/beast/http.hpp>
#include <iostream>
#include <sstream>
namespace http = boost::beast::http;
#define PBLOG_INFO(a) do { std::clog << (a) << std::endl; } while(0);
int main() {
std::string verb = "POST", req_str = "payload";
using http::field;
http::request<http::string_body> request;
request.method_string(verb);
request.target("server_endpoint");
request.version(11);
request.set(field::host, "hostname");
request.set(field::accept, "*/*");
request.set(field::authorization, "authorization_token");
request.set(field::user_agent, "client_name");
request.set(field::content_length, req_str.length());
request.set(field::content_type, "application/x-www-form-urlencoded");
request.set(field::connection, field::close);
request.body() = req_str;
request.prepare_payload();
{
std::ostringstream oss;
oss <<"Request message " << request;
PBLOG_INFO(oss.str());
oss.clear();
}
}
有什么问题?
问题似乎在于Beast的流式操作符重载不适用于任何类型的fast\u ostringstream
。这是出于设计:
no known conversion from 'framework::string_::fast_ostringstream'
to 'std::ostream &' (aka 'basic_ostream<char> &')
然而,很明显,运算符的存储量很大。我用过oss;要打印日志,我们可以点击DestinationURL并使用BoostBeast解析响应。
struct my_cheat_stream {
using Device = boost::iostreams::back_insert_device<std::string>;
using Stream = boost::iostreams::stream<Device>;
std::string buf;
Stream _impl { buf };
template <typename T> my_cheat_stream& operator<<(T&& rhs) {
_impl << std::forward<T>(rhs);
return *this;
}
std::string&& str()&& { _impl.flush(); return std::move(buf); }
std::string_view str()& { _impl.flush(); return buf; }
void clear() {
buf.clear();
_impl.clear(std::ios::failbit|std::ios::badbit);
}
};
template <typename Stream> struct wrap_non_std_stream {
Stream& _wrapped;
explicit wrap_non_std_stream(Stream& ref) : _wrapped(ref) {}
template <typename T> auto& operator<<(T const& rhs) {
_wrapped << rhs;
return *this;
}
auto& operator<<(std::ostream&(*manip)(std::ostream&)) {
_wrapped << manip;
return *this;
}
};
fast_ostringstream oss;
wrap_non_std_stream woss{oss};
woss << "Request message " << request;
woss << std::endl << std::fixed << std::boolalpha << std::flush;