0

I am using cpprestsdk in order to create http client. I need to convert web::json::value like this:

web::json::value obj;
obj[U("1")] = web::json::value(U("123"));
obj[U("2")] = web::json::value(U("321"));

to std::vector<unsigned char>

In order to put it to request

web::http::http_request req(method);
req.set_body(data); <<-- data == std::vector<unsigned char>

and send to the server. I know how to send web::json::value and utility::string_t but have the problem with vector of bytes. So my question is how to convert web::json::value to std::vector<unsigned char>. Thanks.

definename
  • 193
  • 1
  • 12

1 Answers1

0

First off, are you sure you need to convert? The docs say there's a set_body overload for json::value.

If yes, then you can convert to a utf-8 string (that's what the code in set_body does), then copy to a vector.

auto text = utility::conversions::to_utf8string(obj.serialize());

std::vector<unsigned char> data(text.size());
std::transform(text.begin(), text.end(), data.begin(),
    [](char ch)
{
    return static_cast<unsigned char>(ch);
});
Mercury Dime
  • 1,141
  • 2
  • 10
  • 10