3

How should I handle chunked response using cpprestsdk? How to request the next chunk? Is there required functionality at all there?

Here is how we are performing http requests:

web::http::http_request request(web::http::methods::GET);
request.headers().add(LR"(User-Agent)", LR"(ExchangeServicesClient/15.00.0847.030)");
request.headers().add(LR"(Accept)", LR"(text/xml)");
request.set_body(L"request body", L"text/xml");

web::http::client::http_client_config clientConfig;
clientConfig.set_credentials(web::credentials(L"username", L"pass"));
clientConfig.set_validate_certificates(true);

web::http::client::http_client client(L"serviceurl", clientConfig);

auto bodyTask = client.request(request)
    .then([](web::http::http_response response) {
        auto str = response.extract_string().get();
        return str;
});

auto body = bodyTask.get();

If I'm trying naively to perform another request just after this one then I got an error:

WinHttpSendRequest: 5023: The group or resource is not in the correct state to perform the requested operation.

Dmitry Katkevich
  • 883
  • 7
  • 26
  • 1
    looks like they've created a test for this kind of functionality: https://github.com/Microsoft/cpprestsdk/blob/master/Release/tests/functional/http/client/response_stream_tests.cpp#L223 you might have to do some work to direct the resulting stream into your intended destination. – Sam Hatchett Apr 07 '17 at 15:53

1 Answers1

1

In order to read received data in chunks, one needs to get the input stream from the server response

concurrency::streams::istream bodyStream = response.body();

then read continuously from that stream until a given char is found or the number of specified bytes is read

pplx::task<void> repeat(Concurrency::streams::istream bodyStream)
{
Concurrency::streams::container_buffer<std::string> buffer;

return pplx::create_task([=] {
    auto t = bodyStream.read_to_delim(buffer, '\n').get();
    std::cout << buffer.collection() << std::endl;
    return t;
}).then([=](int /*bytesRead*/) {
    if (bodyStream.is_eof()) {
        return pplx::create_task([]{});
    }
    return repeat(bodyStream);
});
}

Here is the full sample: https://github.com/cristeab/oanda_stream

Cristea Bogdan
  • 116
  • 1
  • 11