Debian Codegraph @

third_party/rust/warp/examples/multipart.rs

raw
1 use bytes::BufMut;
2 use futures_util::TryStreamExt;
3 use warp::multipart::FormData;
4 use warp::Filter;
5
6 #[tokio::main]
7 async fn main() {
8 // Running curl -F [email protected] 'localhost:3030/' should print [("file", ".gitignore", "\n/target\n**/*.rs.bk\nCargo.lock\n.idea/\nwarp.iml\n")]
9 let route = warp::multipart::form().and_then(|form: FormData| async move {
10 let field_names: Vec<_> = form
11 .and_then(|mut field| async move {
12 let mut bytes: Vec<u8> = Vec::new();
13
14 // field.data() only returns a piece of the content, you should call over it until it replies None
15 while let Some(content) = field.data().await {
16 let content = content.unwrap();
17 bytes.put(content);
18 }
19 Ok((
20 field.name().to_string(),
21 field.filename().unwrap().to_string(),
22 String::from_utf8_lossy(&*bytes).to_string(),
23 ))
24 })
25 .try_collect()
26 .await
27 .unwrap();
28
29 Ok::<_, warp::Rejection>(format!("{:?}", field_names))
30 });
31 warp::serve(route).run(([127, 0, 0, 1], 3030)).await;
32 }