0

I'm new to Boost and trying to add a POST request to one of the client example. But I failed to read a http::quest<http::file_body> request on the server side.

Assertion failed: body_.file_.is_open(), file D:\Boost C++ Projects\AsioServre\include\boost _1_72_0\boost\beast\http\impl\file_body_win32.hpp, line 184

I read the Boost code in this file_body_win32.hpp file, it just simply tells me the file is open. How come? And the werid thing is if I change content_type: "text/plain" to content_type: "image/jpeg" (body opens an image), then the assertion will be passed, but with body limit exceeded error.

Client Side:

std::string path = "C:\\Users\\Jinx\\Desktop\\Damn\\test.txt";
body.open(path.c_str(), boost::beast::file_mode::scan, ec);
req_to.version(version);
req_to.method(http::verb::post);
req_to.target(target);
req_to.set(http::field::host, host);
req_to.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req_to.set(http::field::content_type, "text/plain");
req_to.content_length(body.size());
req_to.keep_alive(req_.keep_alive());
req_to.body() = std::move(body);
req_to.prepare_payload();

http::async_write(stream_, req_to, beast::bind_front_handler(&session::on_write, shared_from_this()));

On the server side, I have no idea what type of buffer I should use. I've tried a multi buffer, it didn't work either.

Server Side:

beast::tcp_stream stream_;
beast::flat_buffer buffer_; 
std::shared_ptr<std::string const> doc_root_;
http::request<http::file_body> req_from;

http::async_read(stream_, buffer_, req_from, beast::bind_front_handler(&session::on_read, shared_from_this()));

According to the http::file_body document:

When serializing, the implementation will read the file and present those octets as the body content.

How can the file opened by the server?

SpellTheif
  • 715
  • 2
  • 12
  • 26

1 Answers1

0

On the server side, before calling http::async_read(stream_, buffer_, req_from, beast::bind_front_handler(&session::on_read, shared_from_this())); you must open the file, to which the body content should be written to.

To do so do the following:

// Add an error_code, which lives as long as the async operation
boost::beast::error_code ec;

// Then before reading the content of the request do
req_from.body().open("file.jpeg", boost::beast::file_mode::write, ec);
http::async_read(stream_, buffer_, req_from, beast::bind_front_handler(&session::on_read, shared_from_this()));
nils
  • 338
  • 2
  • 11