The following code use to get http response message:
boost::beast::tcp_stream stream_;
boost::beast::flat_buffer buffer;
boost::beast::http::response<boost::beast::http::dynamic_body> res;
boost::beast::http::read(stream_, buffer, res);
However, In some cases, based on the preceding request, I can expect that the response message body will include large binary file.
Therefore, I’d like to read it directly to the filesystem and not through buffer
variable to avoid excessive use of process memory. How can it be done ?
in Objective-c framework NSUrlSession
there's an easy way to do it using NSURLSessionDownloadTask
instead of NSURLSessionDataTask
, so I wonder if it's also exist in boost.
Thanks !
In general, you can use the
http::buffer_body
to handle arbitrarily large request/response messages.If you specifically want to read/write from a filesystem file, you can have the
http::file_body
instead.Full Demo
buffer_body
The documentation sample for
buffer_body
is here https://www.boost.org/doc/libs/1_77_0/libs/beast/doc/html/beast/using_http/parser_stream_operations/incremental_read.html.Using it to write to std::cout: Live On Coliru
Full
file_body
exampleThis is much shorter, writing to
body.html
:Live On Coliru
Prints
With
file body.html; wc body.html
showing:Beyond: streaming to child processes and streaming processing
I have an advanced example of that here: How to read data from Internet using muli-threading with connecting only once?.