Skip to main content

Body

Struct Body 

Source
pub struct Body(/* private fields */);
Expand description

Handle for accessing and mutating an HTTP body stream.

A Body is tied to a specific request or response context, depending on how it is constructed. Use it to read the full buffered body or write a new one.

Implementations§

Source§

impl Body

Source

pub fn read(&self) -> Bytes

Read the entire body into memory and return it as Bytes.

This returns the buffered payload when body buffering is enabled by the host.

feature::BufferRequest is required to read without consuming the request body. To enable it, call admin::enable(BufferRequest) before returning from handle_request. Otherwise, the next handler may panic attempting to read the request body because it was already read.

feature::BufferResponse is required to read the response body produced by the next handler defined on the host inside handle_response. To enable it, call admin::enable(BufferResponse) beforehand. Otherwise, the guest may read EOF because the downstream handler already consumed it.

Examples found in repository?
examples/info.rs (line 23)
17    fn handle_request(&self, request: &Request, _response: &Response) -> (bool, i32) {
18        info!("Request: {} {} {} {}", request.method(), request.version(), request.uri(), request.source_addr());
19        for (name, values) in request.header.entries_iter() {
20            let values = values.iter().map(|v| format!("{v}")).collect::<Vec<_>>().join(", ");
21            info!("Header: {} [{}]", name, values);
22        }
23        info!("Body: {}", request.body.read());
24        (true, 0)
25    }
Source

pub fn write(&self, body: &[u8])

Replace the body with the provided bytes.

Use this to set a new payload after inspecting or transforming the original.

Auto Trait Implementations§

§

impl Freeze for Body

§

impl RefUnwindSafe for Body

§

impl Send for Body

§

impl Sync for Body

§

impl Unpin for Body

§

impl UnsafeUnpin for Body

§

impl UnwindSafe for Body

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.