I'm using an example given in boost/beast prepare_payload()
http::response<http::string_body> res{http::verb::post, "/"};
res.set(http::field::user_agent, "Beast");
res.body() = Respon.dump(4);
res.prepare_payload();
2 things i've changed, name of constant, and data put in body(). (Respon is Json, Respon.dump(4) is string) And i get an error.
Error C2440 'initializing': cannot convert from 'initializer list' to 'boost::beast::http::message<false,boost::beast::http::basic_string_body<char,std::char_traits<char>,std::allocator<char>>,boost::beast::http::fields>' https server D:\tempo\03-lab-08-http-server-Hamsterrhino\sources\source.cpp 171
Why wouldn't an example work? Did the work changed but they forgot to change an example?
You're initializing a response as if it were a request.
A response doesn't have a http verb or request path;
A response constructor can initialize the message and fields, thought it's more conventional to initialize them after construction, and pass the status/version in the constructor:
#include <boost/beast/http.hpp>
#include <boost/beast/http/status.hpp>
#include <iostream>
namespace http = boost::beast::http;
struct {
std::string dump(int) const { return "probably json?"; }
} static Respon;
int main() {
http::response<http::string_body> res(http::status::ok, 11);
res.set(http::field::user_agent, "Beast");
res.body() = Respon.dump(4);
res.prepare_payload();
std::cout << res;
}
Prints
HTTP/1.1 200 OK
User-Agent: Beast
Content-Length: 14
probably json?