no suitable user-defined conversion from utility::string_t to std::string - c++

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();
}
});

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

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.

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));

Related

http_client of cpprestsdk/casablanca

I have api https://api.gm-system.net/api/authenticate/searchStaffs/searchText which return a list staff.
And here is my code to access this api using cpprestsdk with c++.
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/michael"));
return client.request(methods::GET);
})
// Handle response headers arriving.
.then([=](http_response response)
{
......
}
This one if fine. But with that i just manually input the "michael" searchText.
How can I make it that it will accept any searchText something like this.
void MyTest(std::string searchText)
{
..... code here
// Create http_client to send the request.
http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/" + searchText));
return client.request(methods::GET);
..... code here
}
I already tried this it won't work. Some problem with 'U' macro.
From https://github.com/Microsoft/cpprestsdk/wiki/FAQ the discription of U macro is says:
The 'U' macro can be used to create a string literal of the platform type. If you are using a library causing conflicts with the 'U' macro, for example Boost.Iostreams it can be turned off by defining the macro '_TURN_OFF_PLATFORM_STRING' before including the C++ REST SDK header files.
If I point my cursor to U, the error says:
no operator "+" matches these operands operand types are; const wchar_t[57] + const std::string
I hope some can help me. Thanks.
Since
The C++ REST SDK uses a different string type dependent on the
platform being targeted. For example for the Windows platforms
utility::string_t is std::wstring using UTF-16, on Linux std::string
using UTF-8.
you should use the utility::string_t class whenever it is required and don't mix it with std::string or const char * (and use the U macro when in need of a literal).
In other words, your function should accept an utility::string_t as its searchText argument (instead of std::string):
void MyTest(utility::string_t searchText)
{
http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/") + searchText);
// etc ...
}
use it like this:
int main()
{
utility::string_t searchText = U("Michael");
MyTest(searchText);
return 0;
}
If the function has to be called from a platform specific context, the corresponding std type can be used as the passed in argument type (i.e. use std::wstring on Windows):
std::wstring searchText = L"Michael";
MyTest(searchText);

How to print utility::string_t type value? [duplicate]

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();
}
});
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
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.
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));

No suitable method for adding c++ stl string to C++ rest sdk json object utility string_t

I want to add a string into C++ rest sdk json object. I tried
json::value obj;
obj[L"Login"] = json::value::string();
But the problem is this adds only strings as U("str") and not const std::string.
So I can add as
obj[L"Login"] = json::value::string(U("Login"));
but not do as follows:
json::value obj;
string Login= "login";
obj[L"Login"] = json::value::string(Login);
I am using C++ rest sdk 2.8.0.
Try the following type conversion:
json::value obj;
string Login= "login";
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
std::wstring uLogin = converter.from_bytes(Login);
obj[L"Login"] = json::value::string(uLogin);
Better switch all your
std::string
to
std::wstring
objects.
cppRest only works with wide strings, so you will end up converting all over the place...

Conversion between wchar_t* to string

I was trying to create a client in c++ for a web service using a Service Model Metadata Utility Tool, I have established the communication between the two endpoints, but at the client side I receive a wchar_t*, how can i convert it to a string?
Note: the server side is using encoding of UTF-8.
Use this simple function:
std::string wchar2string(wchar_t* str)
{
std::string mystring;
while( *str )
mystring += (char)*str++;
return mystring;
}
I hope that this function can help you!
You can use std::wstring which has a constructor that takes wchar_t*.

Passing a string in Process::Start

I run successfully:
Process::Start("C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe");
from a Windows Forms application (visual c++) and as expected, I got 2 programs running simultaneously. My questions is:
Can I pass a string -indicating the file name- to the Process::Start method? I tried:
std::string str="C:\\Users\\Demetres\\Desktop\\JoypadCodesApplication.exe";
Process::Start("%s", str);
but failed. Is this possible?
EDIT:
I think you actually need to marshal to a System::String^ for passing an argument.
You can even marshal directly from a std::string to a System::String^.
///marshal_as<type>(to_marshal)
///marshal_context ctx; ctx.marshal_as<const char *>(to_marshal)
#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>
#include <msclr/marshal_atl.h>
using namespace msclr::interop;
using namespace System::Runtime::InteropServices;
Process::Start(marshal_as<String^>(str));
But in this case, you can just use a String^ instead:
String^ str = L"path to file";
Process::Start(str);
When you are working with C++/CLI, you either need to marshal back and forth or use the right data type from the start for how you want to use it.
MSDN: Overview of Marshaling in C++
Process::Start expects a String^ and you are attempting to pass it a std::string. It does not have a variadric version, and does not know what a std::string is. To pass the value from a std::string, you must marshal it:
std::string str("Some Path");
Process::Start(marshal_as<String^>(str));