How to access URI query string in server?
How do I get the URI query string from server code? I tried these two but they both come back with empty strings:
void on_open(wspp_server* s, websocketpp::connection_hdl hdl) {
wspp_server::connection_ptr con = s->get_con_from_hdl(hdl);
websocketpp::uri_ptr uri = con->get_uri();
std::string query = uri->get_query();
std::cout << "on_open query = " << query << std::endl;
std::string req_body = con->get_request_body();
std::cout << "on_open request_body = " << req_body << std::endl;
// ...
}
I had the same request, just modify the source code , try to search URL related codes.
you can get the full uri as string
con->get_uri()->str()
if you want the parsed query string alone you may use
boost::regex ex("(http|https|ws|wss)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)"); boost::cmatch what; if (regex_match(strUri.c_str(), what, ex)) { service = std::string(what[1].first, what[1].second); host = std::string(what[2].first, what[2].second); port = std::string(what[3].first, what[3].second); path = std::string(what[4].first, what[4].second); query = std::string(what[5].first, what[5].second); }
you can get the full uri as string
con->get_uri()->str()
Unfortunately con->get_uri()->str() only has the scheme/host/port/path, it doesn't have the query string. The output below returns as shown.
void on_open(wspp_server* s, websocketpp::connection_hdl hdl) {
wspp_server::connection_ptr con = s->get_con_from_hdl(hdl);
websocketpp::uri_ptr uri = con->get_uri();
std::cout << "on_open called, uri = " << uri->str() << std::endl;
std::string query = uri->get_query();
std::cout << "on_open query = " << query << std::endl;
std::string req_body = con->get_request_body();
std::cout << "on_open request_body = " << req_body << std::endl;
// ...
}
on_open called, uri = wss://localhost:9002/
on_open query =
on_open request_body =