Function http_multipart::multipart

source ·
pub fn multipart<Ext, B, T, E>(
    req: &Request<Ext>,
    body: B
) -> Result<Multipart<B>, MultipartError>
where B: Stream<Item = Result<T, E>>, T: AsRef<[u8]>, E: Into<Box<dyn Error + Send + Sync>>,
Expand description

Multipart protocol using high level API that operate over Stream trait.

http crate is used as Http request input. It provides necessary header information needed for Multipart.

Examples:

use std::{convert::Infallible, error, pin::pin};

use futures_core::stream::Stream;
use http::Request;

async fn handle<B>(req: Request<B>) -> Result<(), Box<dyn error::Error + Send + Sync>>
where
    B: Stream<Item = Result<Vec<u8>, Infallible>>
{
    // destruct request type.
    let (parts, body) = req.into_parts();
    let req = Request::from_parts(parts, ());

    // prepare multipart handling.
    let mut multipart = http_multipart::multipart(&req, body)?;

    // pin multipart and start await on the request body.
    let mut multipart = pin!(multipart);

    // try async iterate through fields of the multipart.
    while let Some(mut field) = multipart.try_next().await? {
        // try async iterate through single field's bytes data.
        while let Some(chunk) = field.try_next().await? {
            // handle bytes data.
        }
    }

    Ok(())
}