6

I am using the casablanca C++ Rest library to make HTTP requests.

The problem is that this gives a utility::string_t string as output and I can't quite find any way to convert this to a classic std::string. Any ideas?

client.request(methods::GET).then([](http_response response)
{
  if(response.status_code() == status_codes::OK)
  {
    string_t s = response.extract_string().get();
  }
});
Kaj
  • 2,445
  • 3
  • 23
  • 34

3 Answers3

11

Depending on what platform you are compiling for, the utility::string_t type will be typedef'd to either std::wstring (on Windows) or std::string (on Linux/OSX).

To get a classic utf-8 std::string regardless of platform, take a look at utility::conversions::to_utf8string.

reference documentation

roschuma
  • 536
  • 4
  • 7
  • 1
    This is the only thing tat worked for me on Windows, with C++ REST SDK 2.7.0. The utility::string_t from using value.as_string() was not accepted as std::string. – arie Feb 12 '16 at 09:23
2

If you see the documentation for C++ REST SDK from github, you'll find a typedef

C++ Rest SDK - utility Namespace Reference

typedef std::string     string_t;

So no need to convert it. Both are same types.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • 5
    This only applies on non-Windows. On Windows, this typedef looks like `typedef std::wstring string_t;` – roschuma Mar 28 '16 at 21:54
  • 6
    utility does support the function like `utility::conversions::to_utf8string` – Han Apr 10 '16 at 03:44
2

On Windows Phone 8.1 there is this define:

typedef std::wstring     string_t;

I used this:

string_t toStringT = U("sample");
std::string fromStringT(toStringT.begin(), toStringT.end());

or:

std::string fromStringT(conversions::to_utf8string(toStringT));
Rino Seminara
  • 145
  • 1
  • 7