cpprestsdk / casablanca的http_client

时间:2018-04-17 01:47:50

标签: c++ casablanca cpprest-sdk

我有api https://api.gm-system.net/api/authenticate/searchStaffs/searchText返回列表工作人员。

以下是使用{+ 1}}和c ++访问此API的代码。

cpprestsdk

这个如果罚款。但是我只需手动输入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) { ...... }

我怎样才能接受任何类似这样的searchText。

"michael" searchText

我已经尝试过这个但它不会工作。 “&#39; U&#39;宏。 从https://github.com/Microsoft/cpprestsdk/wiki/FAQ起,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 } 的描述就是:

U macro

如果我将光标指向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. ,则错误显示:

U

我希望有些人可以帮助我。感谢。

1 个答案:

答案 0 :(得分:2)

  

C ++ REST SDK使用不同的字符串类型,具体取决于   平台被定位。例如,对于Windows平台   在Linux std :: string上,utility :: string_t是使用UTF-16的std :: wstring   使用UTF-8。

您应该在需要时使用utility::string_t课程,并且不要将其与std::stringconst char *混合使用(并在需要时使用U课程一个文字)。

换句话说,您的函数应接受utility::string_t作为其searchText参数(而不是std::string):

void MyTest(utility::string_t searchText)
{
    http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/") + searchText);

    // etc ...

}

像这样使用它:

int main()
{

    utility::string_t searchText = U("Michael");
    MyTest(searchText);

    return 0;
}

如果必须从特定于平台的上下文调用该函数,则相应的std类型可以用作传入的参数类型(即在Windows上使用std::wstring):

std::wstring searchText = L"Michael";
MyTest(searchText);