client connection with multiple GET requests
happyturtle123 opened this issue · 4 comments
Hi
With this framework is it possible to connect to a REST SSL endpoint and do multiple GET requests without disconnecting, for example (using the example code as and
this is an excerpt from the SSL example...
http::ssl::client my_https_client{ctx};
const auto & host = "www.google.com";
uint32_t port = 443;
const auto & on_connect = [](auto & session){
http::base::out("Successful connected!");
session.do_handshake();
};
const auto & on_handshake = [&host](auto & session){
http::base::out("Successful handshake!");
boost::beast::http::request<boost::beast::http::string_body> req;
req.version(11); // HTTP 1.1
req.method(boost::beast::http::verb::get); // GET
req.target("/ENDPOINT1");
req.set(boost::beast::http::field::host, host);
req.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
session.do_write(std::move(req));
};
const auto & on_receive = [](auto & res, auto & session){
cout << res << endl;
// session.do_close();
http::base::processor::get().stop();
};
my_https_client.invoke(host, port, on_connect, on_handshake, on_receive);
uint32_t pool_size = boost::thread::hardware_concurrency();
http::base::processor::get().start(pool_size == 0 ? 4 : pool_size << 1);
http::base::processor::get().wait();
return 0;
}
If i make the changes above (not close the session in the on_receive lambda) is there a way i can
do something like send a request to a different endpoint (on the same host) without closing the connection?
I leave it here as a solution...
auto make_request(http::method_t method,
boost::beast::string_view host,
boost::beast::string_view target){
boost::beast::http::request<boost::beast::http::string_body> req;
req.version(11); // HTTP 1.1
req.method(method); // GET
req.target(target);
req.set(boost::beast::http::field::host, host);
req.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
return req;
}
int main(){
...
const auto & on_handshake = [&host](auto & session){
http::base::out("Successful handshake!");
session.do_write(make_request(boost::beast::http::verb::get, host, "/ENDPOINT1"));
};
int ep = 1;
const auto & on_receive = [&host, &ep](auto & res, auto & session){
cout << res << endl;
if(ep == 1){
ep = 2;
session.do_write(make_request(boost::beast::http::verb::get, host, "/ENDPOINT2"));
return;
}
session.do_close();
http::base::processor::get().stop();
};
my_https_client.invoke(host, port, on_connect, on_handshake, on_receive);
...
}
Perfect, this is what i was generally looking for!
One final question, it looks like i can send on the new endpoint "/ENDPOINT2" only if i receive a packet. how would I send to ENDPOINT2 without already in the on_receive handler ?
Many thanks for the prompt reply!
HTTP 1.x is not supported multiple requests over a single connection!
http::ssl::client conn1{ctx};
http::ssl::client conn2{ctx};
// as above
Something like this! :)
thank you for the info!