Quantcast
Channel: WE MOVED to github.com/microsoft/cpprestsdk. This site is not monitored!
Viewing all articles
Browse latest Browse all 4845

New Post: Use of memcpy() function with file_buffer or fstream::istream

$
0
0
Hi utsav_popli,

It is a little confusing what you have going on here. You are opening a file for output, then reading the response body into that file. Then it looks like you are trying to read the file for input through a raw pointer. First off you didn't open the file for input, second you can't get a raw pointer to a file stream.

I think what you are trying to do is get a raw pointer to the memory containing the response stream. Here is one way the code could look:
    http_client client(U("address"));
    http_request request(methods::GET);
    
    // set a stream backed by a vector that the entire response body should be written to
    container_buffer<std::vector<uint8_t>> responseBody; // container_buffer is in cpprest/containerstream.h
    request.set_response_stream(responseBody.create_ostream());

    client.request(request).then([](http_response response)
    {
        // headers have arrived...
        // inspect the status code...

        // return a task that will be completed once entire response body has arrived
        return response.content_ready();
    }).then([responseBody](::pplx::task<http_response> bodyArrivedTask)
    {
        try
        {
            bodyArrivedTask.wait();

            // response body has completely arrived, extract the backing vectory containing the data
            std::vector<uint8_t> body = std::move(responseBody.collection());

            // TODO you now have the response body in a std::vector and do whatever...
        }
        catch (const http_exception &e)
        {
            // handle any exceptions
        }
    });
In this snippet I'm have the response body be directly written into a std::vector. Once completed you have access to the raw memory and can do whatever. This is probably the most efficient because it avoids any additional copies. Here is another similar way, but waits until the response body has arrived and then copies into a vector.
    http_client client(U("address"));
    client.request(methods::GET).then([](http_response response)
    {
        // headers have arrived...
        // inspect the status code...

        // return a task that will be completed once entire response body has arrived
        return response.content_ready();
    }).then([](::pplx::task<http_response> bodyArrivedTask)
    {
        try
        {
            http_response response = bodyArrivedTask.get();

            // read the entire response body into a buffer backed by a vector
            container_buffer<std::vector<uint8_t>> responseBody;
            response.body().read_to_end(responseBody).wait();
            std::vector<uint8_t> body = std::move(responseBody.collection());

            // TODO you now have the response body in a std::vector and do whatever...
        }
        catch (const http_exception &e)
        {
            // handle any exceptions
        }
    });
Hope that helps.

Steve

Viewing all articles
Browse latest Browse all 4845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>