如何从标头中检索不记名令牌rest sdk

问题描述

我使用 REST SDK 工作过几次。我想使用 C++ 从响应标头中检索 Bearer 令牌。

来自邮递员的图片

image

我已经试过了:

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

但我只得到这个:

image

这告诉我我做错了什么。

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

解决方法

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

假设您使用的是 InputBinding(您没有说),它的 Microsoft's REST SDK 类包含 utility::string_t 值,而 utility::string_t 在 Windows 上被定义为 std::wstring (尽管 web::http::http_headers 怎么说),而不是像您期望的那样 std::string

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

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