Skip to main content

BodyExtractExt

Trait BodyExtractExt 

pub trait BodyExtractExt: Sealed {
    // Required methods
    fn try_into_json<T>(
        self,
    ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
       where T: DeserializeOwned + Send + 'static;
    fn try_into_json_streaming<T>(
        self,
    ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
       where T: DeserializeOwned + Send + 'static;
    fn try_capture_json(
        self,
        selectors: impl IntoIterator<Item = JsonPath> + Send,
        max_capture_bytes: usize,
    ) -> impl Future<Output = Result<Vec<OwnedCapturedValue>, Box<dyn Error + Sync + Send>>> + Send;
    fn try_into_string(
        self,
    ) -> impl Future<Output = Result<String, Box<dyn Error + Sync + Send>>> + Send;
    fn try_into_json_with<T>(
        self,
        opts: CollectOptions,
    ) -> impl Future<Output = Result<T, CollectError>> + Send
       where T: DeserializeOwned + Send + 'static;
    fn try_into_string_with(
        self,
        opts: CollectOptions,
    ) -> impl Future<Output = Result<String, CollectError>> + Send;
}
Available on crate features http and std only.
Expand description

An extension trait for StreamingBody that provides methods to extract data from it.

Required Methods§

fn try_into_json<T>( self, ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object.

Buffers the entire body in memory before deserializing. For large bodies prefer BodyExtractExt::try_into_json_streaming.

fn try_into_json_streaming<T>( self, ) -> impl Future<Output = Result<T, Box<dyn Error + Sync + Send>>> + Send
where T: DeserializeOwned + Send + 'static,

Try to deserialize the (contained) body as a JSON object, streaming bytes from the body directly into the JSON parser instead of buffering the whole body first.

Preferable to BodyExtractExt::try_into_json for large bodies where peak memory matters.

§Note

Internally this runs serde_json::from_reader inside a [tokio::task::spawn_blocking] task using SyncIoBridge to bridge the async body to serde’s synchronous io::Read interface. The thread hop is unavoidable today because serde::Deserialize is a pull-based, synchronous trait and no production-ready async-first JSON crate currently ships a drop-in serde::Deserialize integration. Contributions that remove this hop — e.g. by building a serde::Deserializer on top of an async event-based JSON parser — are welcome.

fn try_capture_json( self, selectors: impl IntoIterator<Item = JsonPath> + Send, max_capture_bytes: usize, ) -> impl Future<Output = Result<Vec<OwnedCapturedValue>, Box<dyn Error + Sync + Send>>> + Send

Capture JSON values matching selectors while streaming over the body.

Unlike try_into_json, this does not buffer the whole body. Only selected values are copied, and each selected scalar or object/array subtree is bounded by max_capture_bytes.

fn try_into_string( self, ) -> impl Future<Output = Result<String, Box<dyn Error + Sync + Send>>> + Send

Try to turn the (contained) body in an utf-8 string.

fn try_into_json_with<T>( self, opts: CollectOptions, ) -> impl Future<Output = Result<T, CollectError>> + Send
where T: DeserializeOwned + Send + 'static,

Like try_into_json, but bounded by opts (a size cap and/or timeout).

On success returns the decoded value. Otherwise a CollectError tells you why — cap reached, timed out, stream failure, or decode failure — and for everything but a stream failure CollectError::into_full_body hands the body back so it can be forwarded on untouched (handy for proxies).

Unlike wrapping the body in Limited, hitting the cap here does not destroy the body.

fn try_into_string_with( self, opts: CollectOptions, ) -> impl Future<Output = Result<String, CollectError>> + Send

Like try_into_string, but bounded by opts. See try_into_json_with for the error semantics.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl<B> BodyExtractExt for B
where B: Into<Body> + Send + 'static,

§

impl<Body> BodyExtractExt for Request<Body>
where Body: Body<Data = Bytes> + Send + Sync + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,

§

impl<Body> BodyExtractExt for Response<Body>
where Body: Body<Data = Bytes> + Send + Sync + 'static, <Body as Body>::Error: Into<Box<dyn Error + Sync + Send>>,