首页 > 解决方案 > 如何从标头中检索不记名令牌(rest sdk)

问题描述

我曾多次使用 REST SDK。我想Bearer使用 C++ 从响应标头中检索令牌。

图片来自邮递员: 图片

我试过这个:

CMyClass::SomeMethod(const web::http::http_request& Request)
{
    const web::http::http_headers& headers = Request.headers();
    web::http::http_headers::const_iterator it = headers.find(web::http::header_names::authorization);
    if (it != headers.end())
    {
        std::cout << "+++\t" << it->first.c_str() << "\t" << it->second.c_str() << std::endl;
    }
}

但我只得到这个:

图片

这告诉我我做错了什么。

那么,如何Bearer从标头中检索令牌web::http::http_request

标签: c++restcpprest-sdk

解决方案


您看到的输出意味着您没有将char*字符串指针传递给std::cout.

假设您使用的是Microsoft 的 REST SDK(您没有说),它的web::http::http_headers类包含utility::string_t值,并且在 Windows 上utility::string_t定义std::wstring(尽管文档说),std::string不像您期望的那样。

这意味着您将wchar_t*字符串指针传递给std:::cout,它没有operator<<for wchar_t*,但确实有一个 for void*,因此您会看到输出。

您需要std::wcout改用,或者在打印之前将std::wstring数据转换为。std::string


推荐阅读