Issue
Trying to use boost for http connection. So far I have followed the official guide and I was able to get the response correctly and parse the body:
// Send the HTTP request to the remote host
http::write(stream, req);
// This buffer is used for reading and must be persisted
beast::flat_buffer buffer;
// Declare a container to hold the response
http::response<http::dynamic_body> res;
// Receive the HTTP response
http::read(stream, buffer, res);
// Write the message to standard out
std::cout << res << std::endl;
std::string body { boost::asio::buffers_begin(res.body().data()),
boost::asio::buffers_end(res.body().data()) };
The problem now is that I don't know how to get the header and check the status code. Any help?
Solution
The status code is not a header, and it's just a property of the response object:
std::cout << res.result_int() << std::endl;
std::cout << res.result() << std::endl;
std::cout << res.reason() << std::endl;
for (auto& h : res.base()) {
std::cout << "Field: " << h.name() << "/text: " << h.name_string() << ", Value: " << h.value() << "\n";
}
Prints things like
200
OK
OK
Field: Age/text: Age, Value: 437767
Field: Cache-Control/text: Cache-Control, Value: max-age=604800
Field: Content-Type/text: Content-Type, Value: text/html; charset=UTF-8
Field: Date/text: Date, Value: Tue, 23 Jun 2020 16:43:43 GMT
Field: ETag/text: Etag, Value: "3147526947+ident"
Field: Expires/text: Expires, Value: Tue, 30 Jun 2020 16:43:43 GMT
Field: Last-Modified/text: Last-Modified, Value: Thu, 17 Oct 2019 07:18:26 GMT
Field: Server/text: Server, Value: ECS (bsa/EB15)
Field: Vary/text: Vary, Value: Accept-Encoding
Field: <unknown-field>/text: X-Cache, Value: HIT
Field: Content-Length/text: Content-Length, Value: 1256
Answered By - sehe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.