c++ - 使用 C++ REST SDK 将 Stream 响应转换为 byte[]

标签 c++ rest

我正在使用 C++ REST SDK 从 API 获取响应,并且我需要将 Concurrency::streams::basic_istream 转换为 byte[]

pplx::task<void> GetResponse()
{
    http_client client(url);
    return client.request(methods::GET).then([](http_response response)
    {
        std::wostringstream ss;
        ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
        std::wcout << ss.str();

        auto bodyStream = response.body();
    });
}

bodyStream 的类型为 Concurrency::streams::basic_istream

我得到的响应的content-typeapplication/octet-stream

如何将其转换为byte[]

最佳答案

使用 http_response::extract_vector 怎么样?方法以字节数组形式返回响应正文?

pplx::task<std::vector<unsigned char>> GetResponse()
{
    http_client client(url);
    return client.request(methods::GET).then([](http_response response)
    {
        std::wostringstream ss;
        ss << L"Server returned returned status code " << response.status_code() << L'.' << std::endl;
        std::wcout << ss.str();

        return response.extract_vector();
    });
}

或者如果您不需要标准输出日志记录,则更简单:

pplx::task<std::vector<unsigned char>> GetResponse()
{
    http_client client(url);
    return client.request(methods::GET).get().extract_vector();
}

然后,您可以在返回的 vector 上调用data()来获取指向底层无符号字符数组(相当于字节数组)的指针。

关于c++ - 使用 C++ REST SDK 将 Stream 响应转换为 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41264332/

相关文章:

c++ - 用 ones 和 zeros C++ 创建一个随机的 std::vector

c++ - std::string 未在 Eclipse/CDT 8.0 中解析

json - 没有 html 标签的 Wordpress Restful Api

java - 无效定义异常 : No serializer found for inner class

rest - 我应该使用 POST 还是 PUT 进行登录请求,为什么?

c++ - 半泛型函数

c++ - 如何获得正确数量的 C++ 命令行参数?

c++ - 使用 clang llvm 检查变量是否在任意源位置具有范围

java - 无法使用 HTTPS 连接

javascript - 将文件保存在实体 POD 上