Question: I'm working with [cpprestsdk](https://github.com/Microsoft/cpprestsdk) in a project as a server. I need to read the Body of a put request.

the documentation uses `message.body().read_to_end(buffer);`

I tried this without success.

```
void MyProject::handle_post(http_request message)
{
        Concurrency::streams::streambuf<uint8_t> buffer;
        buffer.alloc(10); // Crashes Here
        message.body().read_to_end(buffer);
        ...

```

What am I doing wrong? How can I allocate and read data from that buffer?

Answer: 
Faced the same issue, I opted for not using `read_to_end` because every solution I found throw an exception. Turns out it's not the easiest way to extract the body.

To extract the data from the http\_message body ( regardless of MIME type ) i am using the following

```cpp
const auto data = message.content_ready().get().extract_vector().get();
const utility::string_t body = { data.begin(), data.end() };

```

The other option which I can offer up if you need an `std::string` in all cases is

```
const auto body = response.content_ready().get().extract_utf8string(true).get();

```

which is also very convenient way to access the data.
You can use this to read the body to a file:

```
    auto fileStream = concurrency::streams::fstream::open_ostream(utility::conversions::to_string_t(filepath), std::ios::out | std::ios::binary).get();

    // Get body from request
    auto bodyStream = request.body();

    // Write input from request body directly to output filestream
    bodyStream.read_to_end(fileStream.streambuf()).wait();
    fileStream.close().wait();

```