Converting strings to string_t

Jan 30, 2015 at 4:39 PM
I'm a new coder trying to integrate Casablanca into an app in order to retrieve values from a page and would like to use a dynamic querystring. I can't quite figure out how to overcome this error at compile time:

Error 8 error C2440: 'initializing' : cannot convert from 'const std::string' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'

The code I'm working with:

const string_t seedtoupload = localseed;
http_client client(U(""));
uri_builder builder(U("/appname/"));
builder.append_query(U("s"), seedtoupload);

localseed is a std::string, and I've tried various conversion gymnastics (converting it to a char array, c string, etc), but I always get a convert failure when I try to cast it into the string_t that the builder requires.

Jan 30, 2015 at 5:27 PM
Hi ActuallySparky,

In the C++ REST SDK we use utility::string_t for most string operations. It is a typedef to a specific string type depending on the platform. For Windows UTF-16 is the preferred string, and the type is a std::wstring. On Linux UTF-8 is used, as a std::string. Some of the APIs support parameters as both UTF-16 and UTF-8, but the uri_builder doesn't.

The library contains a series of string conversion functions that you can use. Take a look at the reference documentation, the one you are looking for here is utility::conversions::to_string_t(...).

Marked as answer by ActuallySparky on 1/30/2015 at 9:48 PM
Jan 31, 2015 at 5:44 AM
Edited Jan 31, 2015 at 5:44 AM
I'm kind of dense when it comes to documentation. I've tried the below approaches:

builder.append_query(U("s"), localseed.to_string_t());


wstring qs = utility::to_string_t(localseed);
builder.append_query(U("s"), localseed);


builder.append_query(U("s"), utility::to_string_t(localseed));

I get either cast errors or error C2039: 'to_string_t' : is not a member of 'utility'

I didn't see to_string_t used in any of the samples, so I'm unsure of exactly how to invoke it.
Jan 31, 2015 at 5:48 AM
Durrrr. I need to not look at code when I'm so tired. Typos abound. Fixed it!

Thanks for the help, Steve!
Feb 3, 2015 at 7:06 PM
Steve it would be really really really nice to use utf8 for all platforms. it is a bit of a mess to ensure convert everything that goes in and out of the sdk to utf-16 on windows. have some very good arguments...

Feb 3, 2015 at 11:57 PM
Hi eske,

We felt it was important to have UTF-16 available as an easy option on Windows, however I do see the merits in having UTF-8 working regardless of the platform. This is tracked in the following issue.

We can't break all existing programs and realistically we won't be going and adding overloads supporting both UTF-8 and UTF-16 for each API in the library. However for some areas of the library this would be possible. Particularly APIs that deal with unbounded user data are good candidates, to avoid additional conversions/copies. A little bit of progress has been made in this area. In the 2.3.0 release we added http_client::request overloads to work with UTF-8 on Windows. In the development branch I've added http_response::extract_utf8string and http_response::extract_utf16string for handling response bodies, this will be in release 2.5.0.

If you have made changes to improve any of the APIs for UTF-8 on Windows let me know, it would be good to have them contributed back into the library.