为什么 beast/boost 回复说 "cannot convert from initializer list" 到...?

Why beast/boost response says, "cannot convert from initializer list" to...?

我正在使用 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 件事,常量名称和 body() 中的数据。 (响应是 Json,Respon.dump(4) 是字符串) 我得到一个错误。

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-lab-08-http-server-Hamsterrhino\sources\source.cpp  171     

为什么示例不起作用?是不是工作改了但是他们忘了改个例子?

您正在像请求一样初始化响应。

响应没有 http 动词或请求路径;

响应构造函数可以初始化消息和字段,认为构造后初始化它们更常规,并在构造函数中传递status/version:

Live On Coliru

#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;
}

版画

HTTP/1.1 200 OK
User-Agent: Beast
Content-Length: 14

probably json?