pub trait FromBody: Sized {
    type Err: Error;

    // Required method
    fn from_body(body: Bytes, content_type: Mime) -> Result<Self, Self::Err>;
}
Expand description

This trait should be implemented for every type that can be built from an HTTP request body plus its media type.

For most use cases it is sufficient to derive this trait, you usually don’t need to manually implement this. Therefore, make sure that the first variable of your struct can be built from [Bytes], and the second one can be build from Mime. If you have any additional variables, they need to be Default. This is an example of such a struct:

#[derive(FromBody, RequestBody)]
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
struct RawImage {
	content: Vec<u8>,
	content_type: Mime
}

Required Associated Types§

source

type Err: Error

The error type returned by the conversion if it was unsuccessfull. When using the derive macro, there is no way to trigger an error, so std::convert::Infallible is used here. However, this might change in the future.

Required Methods§

source

fn from_body(body: Bytes, content_type: Mime) -> Result<Self, Self::Err>

Perform the conversion.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: DeserializeOwned> FromBody for T

§

type Err = Error

source§

impl<T: for<'a> From<&'a [u8]>> FromBody for Raw<T>