如何在 boost-beast http 请求中设置 http header?
How to set up http header in boost-beast http request?
我正在尝试使用 boost http 库发送带有 header 的消息。我搜索了一种使用 header 发送消息的方法,但找不到。
我想做的是关注
auto const results = resolver.resolve(host, port);
beast::get_lowest_layer(stream).connect(results);
stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
// set http header ("key" = "I am a header")
// I want to add above code.
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
请告诉我将 header 添加到 boost-beast http 请求的正确方法。谢谢!
刚刚
req.set("key", "I am a header");
它与另一个几乎相同 - 标准 HTTP - header,但使用自定义名称。
#include <boost/beast/http.hpp>
#include <iostream>
namespace http = boost::beast::http;
int main() {
auto verb = http::verb::get;
std::string query = "/path";
std::string data = "?whatever=more";
std::string host = "example.com";
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
req.set("key", "I am a header");
req.prepare_payload();
std::cout << req;
}
版画
GET /path?whatever=more HTTP/1.1
Host: example.com
key: I am a header
我正在尝试使用 boost http 库发送带有 header 的消息。我搜索了一种使用 header 发送消息的方法,但找不到。
我想做的是关注
auto const results = resolver.resolve(host, port);
beast::get_lowest_layer(stream).connect(results);
stream.handshake(ssl::stream_base::client);
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
// set http header ("key" = "I am a header")
// I want to add above code.
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
请告诉我将 header 添加到 boost-beast http 请求的正确方法。谢谢!
刚刚
req.set("key", "I am a header");
它与另一个几乎相同 - 标准 HTTP - header,但使用自定义名称。
#include <boost/beast/http.hpp>
#include <iostream>
namespace http = boost::beast::http;
int main() {
auto verb = http::verb::get;
std::string query = "/path";
std::string data = "?whatever=more";
std::string host = "example.com";
http::request<http::string_body> req(verb, query + data, 11);
req.set(http::field::host, host);
req.set("key", "I am a header");
req.prepare_payload();
std::cout << req;
}
版画
GET /path?whatever=more HTTP/1.1
Host: example.com
key: I am a header