0

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.

noyruto88
  • 767
  • 2
  • 18
  • 40
  • Can you please, somehow, point me at the `http_client` class header you're using? I'm curious to see the constructor overloads. – p-a-o-l-o Apr 17 '18 at 08:01
  • I think we can use append or append_path? https://microsoft.github.io/cpprestsdk/classweb_1_1uri__builder.html – noyruto88 Apr 17 '18 at 08:25

1 Answers1

2

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);
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Thank you, but both solution don't work. In the second solution the error says: error C2664: 'web::http::client::http_client::http_client(const web::http::client::http_client &)': cannot convert argument 1 from 'std::basic_string,std::allocator>' to 'const web::uri &'. – noyruto88 Apr 17 '18 at 07:57
  • Ok, I think I solved the mistery (I'm on linux, obviously). – p-a-o-l-o Apr 17 '18 at 09:07
  • Thank you sir. It solve the problem for this. But I'm calling 'MyTest()' function from my CLR project which I cannot use 'utility' there. If I will, I'll get back to the error about 'Mutex'. Is there utility-like for CLR project which uses .NET? – noyruto88 Apr 17 '18 at 09:39
  • @noyruto88 please check my edit, I hope it works fine. – p-a-o-l-o Apr 17 '18 at 09:48
  • Still the same sir. Ahmm anyway I'm getting data from String ^ searchText = searchBox->Text;. Do you know what conversion do i need so that i can pass that data to MyTest(searchText)? – noyruto88 Apr 17 '18 at 10:00
  • https://stackoverflow.com/questions/14047414/convert-a-string-to-wstring-c – p-a-o-l-o Apr 17 '18 at 10:06
  • Hi Sir, maybe you can help me with this https://stackoverflow.com/questions/49933541/how-to-extract-specific-data-returned-from-webjsonvalueserialize-with-cp thank you – noyruto88 Apr 20 '18 at 03:36
  • Hi Sir, do you have an idea about this? https://stackoverflow.com/questions/50406621/cpprest-with-japanese-character . Thank you. – noyruto88 May 18 '18 at 08:11