ntex_multipart/
extractor.rs

1//! Multipart payload support
2use std::convert::Infallible;
3
4use ntex::http::Payload;
5use ntex::web::{FromRequest, HttpRequest};
6
7use crate::server::Multipart;
8
9/// Get request's payload as multipart stream
10///
11/// Content-type: multipart/form-data;
12///
13/// ## Server example
14///
15/// ```rust
16/// use futures::{Stream, StreamExt};
17/// use ntex::web::{self, HttpResponse, Error};
18/// use ntex_multipart as mp;
19///
20/// async fn index(mut payload: mp::Multipart) -> Result<HttpResponse, Error> {
21///     // iterate over multipart stream
22///     while let Some(item) = payload.next().await {
23///            let mut field = item?;
24///
25///            // Field in turn is stream of *Bytes* object
26///            while let Some(chunk) = field.next().await {
27///                println!("-- CHUNK: \n{:?}", std::str::from_utf8(&chunk?));
28///            }
29///     }
30///     Ok(HttpResponse::Ok().into())
31/// }
32/// # fn main() {}
33/// ```
34impl<Err> FromRequest<Err> for Multipart {
35    type Error = Infallible;
36
37    #[inline]
38    async fn from_request(
39        req: &HttpRequest,
40        payload: &mut Payload,
41    ) -> Result<Multipart, Infallible> {
42        Ok(Multipart::new(req.headers(), payload.take()))
43    }
44}