向cppnet-lib响应对象添加一个标头

Add a header to a cppnet-lib response object

本文关键字:一个 cppnet-lib 响应 对象 添加      更新时间:2023-10-16

我想添加一个标题到cppnet-lib basic_response对象。但是,我得到编译错误。

我可以给basic_request添加一个头文件,如下所示:

boost::network::http::basic_request<boost::network::http::tags::http_server> request;
request << header("test", "test");

但是,对如下响应对象执行相同的操作会收到编译错误:

boost::network::http::basic_response<boost::network::http::tags::http_server> response;
response << header("test", "test");

编译错误:

 'headers_container_type': the symbol to the left of a '::' must be a type (header.hpp)
 'value_type': is not a member of boost::network::http::basic_response<boost::network::http::tags::http_server> (header.hpp)
 syntax error: missing ';' before identifier 'value_type' (header.hpp)

这将表明这在响应对象上是不可能的,但下面的页面似乎表明这是可能的。很明显我哪里出错了!

文档:http://cpp-netlib.org/0.8/reference_http_response.html

我的环境是:

    Visual Studio 2013 (build as Release)
  • 增加1.55
  • cppnet-lib: 0.11.0

任何帮助将非常感激!谢谢。

首先,我认为您使用了错误的文档,因为您使用的是0.11.0版本(我建议您使用0.11.1,它在0.11.0的基础上修复了许多错误)。下面是您实际需要的0.11.0文档的链接:

http://cpp-netlib.org/0.11.0/reference/http_server.html响应对象

第二,您想要直接向响应对象的headers成员添加报头。不需要使用函数:

struct my_server {
  void operator(server::request const& req, server::response & res) {
    // do something with the request and/or response
    res.headers.push_back(server::response::header("Name", "value"));
    // do more things
  }
  // ...
};