Closed Milerius closed 5 years ago
I think i should use const auto qp = restinio::parse_query( req->header().query() );
But i was thinking it"s was for optional header, not query
Step 1. Use parse_query
to split query string to key-value pais:
const auto qp = restinio::parse_query( req->header().query() );
Step 2. Use methods of restinio::query_string_params_t
to check the presence of a key. See https://stiffstream.com/en/docs/restinio/0.5/querystringparams.html and https://stiffstream.com/en/docs/restinio/0.5/valueor.html for more details and example.
Yeah i'm on it, by the way, the library is incredibly well coded in modern C++ with a very nice use of modern and clear feature.
@eao197 How we convert the string_view result into a std::string
?
.data() is undefined behavior according to the standard. (no \0
)
restinio::request_handling_status_t
price::get_price(restinio::request_handle_t req, restinio::router::route_params_t)
{
VLOG_SCOPE_F(loguru::Verbosity_INFO, pretty_function);
DVLOG_F(loguru::Verbosity_INFO, "http call: %s", "/api/v1/getprice");
const auto qp = restinio::parse_query(req->header().query());
if (qp.size() != 2) {
DVLOG_F(loguru::Verbosity_ERROR, "Not enough parameters, require base_currency and quote_currency parameters");
return req->create_response(restinio::status_bad_request()).done();
}
if (!qp.has("base_currency") || !qp.has("quote_currency")) {
DVLOG_F(loguru::Verbosity_ERROR, "Wrong parameters, require base_asset and quote_asset parameters");
return req->create_response(restinio::status_unprocessable_entity()).done();
}
std::string res = qp["base_currency"]; //! not compiling i need it as string for filling my structure antara::pair
antara::pair currency_pair{asset{st_symbol{}}, asset{st_symbol{}}};
return req->create_response(restinio::status_ok()).set_body("Done").done();
}
std::string(qp["base_currency"]); ???
What's about:
std::string res = restinio::cast_to<std::string>(qp["base_currency"]);
?
std::string(qp["base_currency"]); this compile and seem's to work, do you suggest i use cast_to ?
std::string(qp["base_currency"]);
It depends on your C++ version. In C++17 and in new compilers std::string
has a constructor that accepts string_view
(and in that case restinio::string_view_t
is just a synonym for std::string_view
). If you use C++14 and rather old compiler cast_to
is the solution.
I'm using c++17, i will then use the constructor thank's a lot !
Hello i'm looking the sample,
I try to retrieve params from a getter url:
/api/restcall?option1=value&option2=value
How to retrieve
option1
andoption2
in the code ?