I'm writing a boost::beast
server, and I'm having trouble figuring out how to use the different request flavours.
When a connection is accepted, I do this to read the request:
- I call
async_read()
, which populates myrequest<string_body>
- I look at the request's method and target to dispatch it to whoever can handle that request
using namespace boost::beast::http;
using namespace boost::beast;
class Session : public enable_shared_from_this<Session>
{
tcp_steam stream;
flat_buffer buf;
request<string_body> req;
public:
Session(boost::asio::ip::tcp::socket&& socket)
: stream(std::move(socket) ) {}
void Read() {
req = request<string_body>{}
async_read(stream, buf, req,
bind_front_handler(&Session::OnRead, shared_from_this() )
);
}
void OnRead(error_code ec, size_t)
{
auto method = GetRoute( req.target(), req.method() );
method( std::move(req) );
}
};
However, some of those methods (like when a user wants to upload/POST
a binary file) I suspect would work better if they received a request<file_body>
Can I convert the request<string_body> to a request<file_body> in those methods?
If not, how can I know the request's method/target before creating the request object? It doesn't seem like there is a way to call async_read
without knowing the Body
in request<Body>
ahead of time.