---
title: Body
summary: An HTTP body that can be read from, written to, or appended to another body.
url: https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/class.Body/
---

**Kind:** Class  
**Module:** [http](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/)  
**Package:** compute-sdk-cpp 0.7.0  
**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L49

Include `fastly/http/body.h` to use this class.

Extends `std::iostream`, `std::streambuf`.

An HTTP body that can be read from, written to, or appended to another body.

The most efficient ways to read from and write to the body are through the `iostreams` implementations. If you want a non-panicking interface, you can use [`Body::read`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/class.Body/#method-read) and [`Body::write`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/class.Body/#method-write) directly, instead.

Read and write operations to a [`Body`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/class.Body/) are automatically buffered.

## Constructor

_Overload 1 of 6_

```cpp
fastly::http::Body::Body()
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L65

Get a new, empty HTTP body.

_Overload 2 of 6_

```cpp
fastly::http::Body::Body(Body &&old)
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L70

**Parameters**

- `old`: `Body &&`

_Overload 3 of 6_

```cpp
fastly::http::Body::Body(std::vector<uint8_t> body_vec)
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L80

**Parameters**

- `body_vec`: `std::vector<uint8_t>`

_Overload 4 of 6_

```cpp
fastly::http::Body::Body(std::string body_str)
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L87

**Parameters**

- `body_str`: `std::string`

_Overload 5 of 6_

```cpp
fastly::http::Body::Body(const char *body_str)
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L88

**Parameters**

- `body_str`: `const char *`

_Overload 6 of 6_

```cpp
fastly::http::Body::Body(std::string_view body_str)
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L89

**Parameters**

- `body_str`: `std::string_view`

## Methods

### append

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L108

```cpp
void fastly::http::Body::append(Body other)
```

Append another body onto the end of this body.

**Parameters**

- `other`: `Body`

**Returns**

`void`

### append_trailer

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L111

```cpp
fastly::expected<void> fastly::http::Body::append_trailer(std::string_view header_name, std::string_view header_value)
```

Appends request or response trailers to the body.

**Parameters**

- `header_name`: `std::string_view`
- `header_value`: `std::string_view`

**Returns**

`fastly::expected<void>`

### read

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L97

```cpp
fastly::expected<size_t> fastly::http::Body::read(uint8_t *buf, size_t bufsize)
```

Read bytes from the body, and return the number of bytes read. Bytes read will be `0` when the [Body](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/http/class.Body/) is or has become empty.

Unlike `operator<<`, this operation will return a `fastly::unexpected<FastlyError>` that can then be handled if the read itself fails, instead of aborting the entire process.

**Parameters**

- `buf`: `uint8_t *`
- `bufsize`: `size_t`

**Returns**

`fastly::expected<size_t>`

### take_body_string

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L115

```cpp
std::string fastly::http::Body::take_body_string()
```

Take the entire body as a string.

**Returns**

`std::string`

### write

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/http/body.h#L105

```cpp
fastly::expected<size_t> fastly::http::Body::write(uint8_t *buf, size_t bufsize)
```

Write bytes to the end of this body, and return the number of bytes written. Bytes written will be `0` on EOF.

Unlike `operator<<`, this operation will return a `fastly::unexpected<FastlyError>` that can then be handled if the write itself fails, instead of aborting the entire process.

**Parameters**

- `buf`: `uint8_t *`
- `bufsize`: `size_t`

**Returns**

`fastly::expected<size_t>`
