Read data from response into memory

Jul 18, 2013 at 6:31 PM
Hi,
    What could be the preferred way for reading the data from response into the memory where response may contain any type of data (string, image, audio or video etc)?
Thanks,
AKshat
Coordinator
Jul 22, 2013 at 3:39 PM
Hi Akshat,

Fundamentally the body of an HTTP request or response just comes in a series of bytes. It is up to you, most likely based on the Content-Type header, to figure out how to treat the data. With our http library at the lowest level you can directly get access to a stream of the http request/response body. Take a look at the API ::web::http::http_request::body(). On top of that we have convenience methods to help get the body into some commonly used data structures. These methods help for when dealing with strings, JSON objects, or raw vector of bytes. Take a look at the methods http_response::extract_string, http_response::extract_json, and http_response::extract_vector.

Thanks,
Steve
Jul 22, 2013 at 4:02 PM
Hello Steve,
           If feasible can you please provide the example of getting the response body using http_response::extract_vector?
Also, I am using NuGet to add the C++ REST SDK to a VS project. It was working fine, but recently i cannot add it using Nuget and get the error
"cannot open source file http_client.h"
IS there any extra care that i need to take?

Thanks,
Akshat
Coordinator
Jul 22, 2013 at 4:29 PM
Edited Jul 22, 2013 at 4:36 PM
Hi Akshat,

In our 1.1.0 release we fixed an issue making an improvement to how we store and organize header files. Now our header files are located in a subfolder. So for example for the http_client.h you should now use:
#include <cpprest/http_client.h>
As far as examples for using extract_vector, it is fairly straightforward. You can take a look at the test cases we have for the extract_* methods as well in response_extract_tests.cpp. Here is a quick snippet as well that I just put together.
http_client client(U("http://myserver"));
client.request(methods::GET).then([](http_response response) -> pplx::task<std::vector<unsigned char>>
{
    return response.extract_vector();
}).then([](std::vector<unsigned char> responseBody)
{
    // TODO - Process the response body here.
})
// Waiting for everything to complete.
// This is blocking so depending on our application and circumstance this might not be a good idea.
.wait();
Steve
Jul 22, 2013 at 4:34 PM
Thank you Steve.
By the way the link that you have provided is directing to the current page itself.

Akshat
Coordinator
Jul 22, 2013 at 4:37 PM
My bad somehow much of ended up with a copy and paste error. I just updated the link.

Thanks,
Steve