How do I get "Transfer-Encoding: chunked" work?

Dec 11, 2015 at 1:31 PM
Getting chunked data works automatically in libcurl, but it doesnt seem to work for cpprest. Do you support it? How do I get the following example to work:

#include <fstream>
#include <cpprest/http_client.h>

std::string GetData_Casab(const std::string& url_in)
{
    std::string ret;

    try
    {
        auto url = utility::conversions::utf8_to_utf16(url_in);

        web::http::http_request request;

        request.set_method(web::http::methods::GET);

        web::http::client::http_client_config client_config;

        web::http::client::http_client client(url, client_config);

        auto resp = client.request(request)
            .then([](web::http::http_response response)
        {
            auto status_code = response.status_code();

            if (status_code != web::http::status_codes::OK) {
                //throw std::exception("http status code not OK");
            }

            const auto& header_res = response.headers();
            utf16string encoding;
            header_res.match(L"Transfer-Encoding", encoding);

            if (encoding == L"chunked") {
                std::cout << "Chunked data found";
                //how to do this?
            }

            return response.extract_utf8string();
        });

        ret = resp.get(); //blocking
    }
    catch (const web::http::http_exception& exc) {
    }

    return ret;
}

int main()
{
    std::string url = "https://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx?0.18193039251491427";
    std::string str = GetData_Casab(url);

    if (str.empty()) {
        std::cout << "Not working!";
    }
    else {
        std::ofstream ofs("chunkedimage.jpg", std::ofstream::out | std::ofstream::binary);
        ofs << str;
        ofs.close();
    }

    return 0;
}
Dec 16, 2015 at 3:58 PM
bump.

Does anyone know if cpprest supports "Chunked transfer encoding"?

https://en.wikipedia.org/wiki/Chunked_transfer_encoding

Thanks /petke
Coordinator
Dec 18, 2015 at 9:20 PM
Hi petke,

We do support chunked encoding. I ran your program, and the error you're getting is actually because you're calling extract_utf8string() on a binary payload. We sanity check the Content-Type to ensure you're not pulling binary data as a string or a string as json. To override this check, you'll need to pass true to extract_utf8string():
            if (encoding == L"chunked") {
                std::cout << "Chunked data found";
                //how to do this?
            }

            return response.extract_utf8string(true);
        });
The created jpg was corrupted for me; some erroneous string conversion may be the cause there. You should probably use extract_vector() instead, since you're grabbing binary data.

In the future, please post on the GitHub project instead (you'll get a faster reply :)).

roschuma