0

I am using cpprestsdk (casabalanca) to POST a request to a server and I have a list of parameters

std::list<std::pair<string, string>> query;
query.push_back(std::make_pair("val1", "one two"));
query.push_back(std::make_pair("val2", "yo"));

that needs to be encoded as form-encoded parameters.

val1=one%20two&val2=yo

The problem I cannot find a Api to do that (like I have web::json::value for a json payload). I need to encode each key/value and do the concatenation myself. There is an Api I am missing out or this simply doesn't exist ?

cprogrammer
  • 5,503
  • 3
  • 36
  • 56

2 Answers2

0

Found the solution...

web::http::http_request request;

web::uri_builder parameter;
parameter.append_query("val1", "one two", true);
parameter.append_query("val2", "yo", true);

request.set_body(parameter.query(), web::http::details::mime_types::application_x_www_form_urlencoded);
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
0

You can use the std::list and fill query parameter

std::string test_url("http://127.0.0.1:9044/api/v1/somepath/");
web::uri_builder ub(test_url);            

std::list<std::pair<std::string, std::string>> query;
query.push_back(std::make_pair("val1", "one two"));
query.push_back(std::make_pair("val2", "yo"));


for(auto const &it: query)
{
    std::cout<<it.first<<" : "<<it.second<<std::endl;
    ub.append_query(it.first, it.second);
}
std::string uri_builder_str = ub.to_string();
http_client client(uri_builder_str);
std::cout<<uri_builder_str<<std::endl;
return client.request(methods::GET);
Renju Ashokan
  • 428
  • 6
  • 18