---
title: BodyHandle
summary: A low-level interface to HTTP bodies.
url: https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/handle/struct.BodyHandle/
---

**Kind:** Struct  
**Module:** [handle](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/handle/)  
**Package:** fastly 0.13.1  
**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#30

```rust
pub struct BodyHandle { /* private fields */ }
```

A low-level interface to HTTP bodies.

For most applications, you should use [`Body`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/body/struct.Body/) instead of this
interface. See the top-level [`handle`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/handle/) documentation for more details.

This type implements [`Read`](https://doc.rust-lang.org/stable/alloc/io/read/trait.Read.html) to read bytes from the beginning of a body, and [`Write`](https://doc.rust-lang.org/stable/core/io/write/trait.Write.html) to write
to the end of a body. Note that these operations are unbuffered, unlike the same operations on
the higher-level [`Body`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/body/struct.Body/) type.

## Constants

### INVALID

**Type:** `Self`  
**Flags:** read-only  
**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#49

An invalid body handle.

This is primarily useful to represent uninitialized values when using the interfaces in
[`fastly_sys`](https://docs.rs/fastly_sys/latest/fastly_sys/index.html).

## Methods

### append

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#202

```rust
fn append(&mut self, other: BodyHandle)
```

Append another body onto the end of this body.

This operation is performed in amortized constant time, and so should always be preferred to reading
an entire body and then writing the same contents to another body.

The other body will no longer be valid after this call.

**Parameters**

- `self`: `&mut Self`
- `other`: `BodyHandle`

### as_u32

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#123

```rust
unsafe fn as_u32(&self) -> u32
```

Get the underlying representation of the handle.

#### Safety

This should only be used when calling the raw ABI directly, and care should be taken not to
reuse or alias handle values.

**Parameters**

- `self`: `&Self`

**Returns**

`u32`

### as_u32_mut

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#140

```rust
unsafe fn as_u32_mut(&mut self) -> &mut u32
```

Get a mutable reference to the underlying `u32` representation of the handle.

#### Safety

This should only be used when calling the raw ABI directly, and care should be taken not to
reuse or alias handle values.

**Parameters**

- `self`: `&mut Self`

**Returns**

`&mut u32`

### close

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#275

```rust
fn close(self) -> Result<(), HandleError>
```

Close the BodyHandle by removing it from the host Session. This will close
out streaming and non streaming bodies. Care must be taken to only
close out the handle if the body is not being written to or read from
anymore. If the handle has already been closed an error will be
returned.

#### Examples

```rust
let mut body = BodyHandle::new();
body.write_all(b"You're already closed.")?;
// The handle is not being used in a request and doesn't refer to any
// response body so we can close this out
body.close()?;
```

**Parameters**

- `self`: `Self`

**Returns**

`Result<(), HandleError>`

### from_client

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#174

```rust
fn from_client() -> Self
```

Get a handle to the client request body.

This handle may only be retrieved once per execution, either through this function or
through [`client_request_and_body()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/handle/fn.client_request_and_body/).

**Returns**

`Self`

### from_u32

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#90

```rust
unsafe fn from_u32(handle: u32) -> Self
```

Make a handle from its underlying representation.

#### Safety

This should only be used when calling the raw ABI directly, and care should be taken not to
reuse or alias handle values.

**Parameters**

- `handle`: `u32`

**Returns**

`Self`

### into_bytes

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#211

```rust
fn into_bytes(self) -> Vec<u8>
```

Read the entirety of the body into a byte vector.

#### Memory usage

This method will cause the entire body to be buffered in WebAssembly memory. You should take
care not to exceed the WebAssembly memory limits, and consider using [`Read`](https://doc.rust-lang.org/stable/alloc/io/read/trait.Read.html) methods to
control how much of the body you process at once.

**Parameters**

- `self`: `Self`

**Returns**

`Vec<u8>`

### into_string

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#227

```rust
fn into_string(self) -> String
```

Read the entirety of the body into a `String`, interpreting the bytes as UTF-8.

#### Memory usage

This method will cause the entire body to be buffered in WebAssembly memory. You should take
care not to exceed the WebAssembly memory limits, and consider using [`Read`](https://doc.rust-lang.org/stable/alloc/io/read/trait.Read.html) methods to
control how much of the body you process at once.

#### Panics

If the body does not contain a valid UTF-8 string, this function will panic. To explicitly handle
the possibility of invalid UTF-8 data, use [`into_bytes()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/handle/struct.BodyHandle/#method-into_bytes) and then convert
the bytes explicitly with a function like [`String::from_utf8`](https://doc.rust-lang.org/stable/alloc/string/String/fn.from_utf8.html).

**Parameters**

- `self`: `Self`

**Returns**

`String`

### into_u32

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#157

```rust
fn into_u32(self) -> u32
```

Turn a handle into its representation without closing the underlying resource.

#### Safety

This should only be used when calling the raw ABI directly, and care should be taken not to
reuse or alias handle values.

**Parameters**

- `self`: `Self`

**Returns**

`u32`

### is_invalid

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#73

```rust
const fn is_invalid(&self) -> bool
```

Returns `true` if the body handle is invalid.

**Parameters**

- `self`: `&Self`

**Returns**

`bool`

### is_valid

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#61

```rust
const fn is_valid(&self) -> bool
```

Returns `true` if the body handle is valid.

**Parameters**

- `self`: `&Self`

**Returns**

`bool`

### new

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#188

```rust
fn new() -> Self
```

Acquire a new, empty body handle.

**Returns**

`Self`

## Trait implementations

### impl BodyHandleExt for BodyHandle

#### get_trailer_names

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#534

```rust
fn get_trailer_names<'a>(&'a self, buf_size: usize) -> Result<Box<(dyn Iterator<Item = Result<HeaderName, BufferSizeError>> + 'a)>, BodyHandleError>
```

**Parameters**

- `self`: `&Self`
- `buf_size`: `usize`

**Returns**

`Result<Box<(dyn Iterator<Item = Result<HeaderName, BufferSizeError>> + 'a)>, BodyHandleError>`

#### get_trailer_value

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#542

```rust
fn get_trailer_value(&self, name: &HeaderName, max_len: usize) -> Result<Result<Option<HeaderValue>, BufferSizeError>, BodyHandleError>
```

**Parameters**

- `self`: `&Self`
- `name`: `&HeaderName`
- `max_len`: `usize`

**Returns**

`Result<Result<Option<HeaderValue>, BufferSizeError>, BodyHandleError>`

#### get_trailer_values

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#578

```rust
fn get_trailer_values<'a>(&'a self, name: &'a HeaderName, max_len: usize) -> Result<Box<(dyn Iterator<Item = Result<HeaderValue, BufferSizeError>> + 'a)>, BodyHandleError>
```

**Parameters**

- `self`: `&Self`
- `name`: `&HeaderName`
- `max_len`: `usize`

**Returns**

`Result<Box<(dyn Iterator<Item = Result<HeaderValue, BufferSizeError>> + 'a)>, BodyHandleError>`

### impl Debug for BodyHandle

#### fmt

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#27

```rust
fn fmt(&self, f: &mut Formatter<'_>) -> Result
```

**Parameters**

- `self`: `&Self`
- `f`: `&mut $crate::fmt::Formatter`

**Returns**

`$crate::fmt::Result`

### impl Drop for BodyHandle

#### drop

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#492

```rust
fn drop(&mut self)
```

**Parameters**

- `self`: `&mut Self`

### impl From<&[u8]> for BodyHandle

#### from

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#518

```rust
fn from(s: &[u8]) -> Self
```

**Parameters**

- `s`: `&[u8]`

**Returns**

`Self`

### impl From<&str> for BodyHandle

#### from

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#502

```rust
fn from(s: &str) -> Self
```

**Parameters**

- `s`: `&str`

**Returns**

`Self`

### impl From<String> for BodyHandle

#### from

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#512

```rust
fn from(s: String) -> Self
```

**Parameters**

- `s`: `String`

**Returns**

`Self`

### impl From<Vec<u8>> for BodyHandle

#### from

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#528

```rust
fn from(s: Vec<u8>) -> Self
```

**Parameters**

- `s`: `Vec<u8>`

**Returns**

`Self`

### impl Hash for BodyHandle

#### hash

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#28

```rust
fn hash<__H: Hasher>(&self, state: &mut __H)
```

**Parameters**

- `self`: `&Self`
- `state`: `&mut __H`

### impl PartialEq for BodyHandle

#### eq

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#28

```rust
fn eq(&self, other: &Self) -> bool
```

**Parameters**

- `self`: `&Self`
- `other`: `&Self`

**Returns**

`bool`

### impl Read for BodyHandle

#### read

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#445

```rust
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
```

**Parameters**

- `self`: `&mut Self`
- `buf`: `&mut [u8]`

**Returns**

`std::io::Result<usize>`

### impl Write for BodyHandle

#### flush

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#486

```rust
fn flush(&mut self) -> Result<()>
```

**Parameters**

- `self`: `&mut Self`

**Returns**

`std::io::Result<()>`

#### write

**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/http/body/handle.rs#466

```rust
fn write(&mut self, buf: &[u8]) -> Result<usize>
```

**Parameters**

- `self`: `&mut Self`
- `buf`: `&[u8]`

**Returns**

`std::io::Result<usize>`

## Auto trait implementations

- `Freeze`
- `RefUnwindSafe`
- `Send`
- `Sync`
- `Unpin`
- `UnsafeUnpin`
- `UnwindSafe`

## Blanket implementations

- `Any`
- `Borrow<T>`
- `BorrowMut<T>`
- `Downcast`
- `DowncastSync`
- `Equivalent<K>`
- `ErasedDestructor`
- `From<T>`
- `Into<U>`
- `IntoEither`
- `Same`
- `TryFrom<U>`
- `TryInto<U>`
