New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support of constructing multipart/form-data requests #126
Comments
@nsavch this would be great, and I think wrapping the ExampleI haven't properly researched this yet, but one way to think about multipart is as a "response containing multiple bodies". I wonder if we could reuse Requestlet mut req = Request::new(Method::Get, "http://example.website");
let mut multi = Multipart::new();
multi.push("hello world");
multi.push(Body::from_file("./cats.jpeg").await?);
req.set_body(multi); Responselet mut res = Response::new(200); // get this from somewhere
for body.await? in res.body_multipart() { // pretending async iteration works
println!("file: {}", body.file_name()); // new method; Option<&str>
println!("content: {}", body.into_string()?);
} |
Another crate that could be looked at for inspiration is multer, which is an async multipart parser. It's what I currently use for a project. Somewhat annoyingly it currently requires either an futures use std::pin::Pin;
use async_std::stream::Stream;
use async_std::task::{Context, Poll};
use futures_io::AsyncRead;
struct ByteStream<R>(R);
impl<R: Unpin + AsyncRead> Stream for ByteStream<R> {
type Item = Result<Vec<u8>, futures_io::Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let mut buf = [0u8; 1024];
match Pin::new(&mut self.0).poll_read(cx, &mut buf[..]) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(0)) => Poll::Ready(None),
Poll::Ready(Ok(n)) => Poll::Ready(Some(Ok(buf[..n].to_vec()))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
}
}
} |
It seems to me that we may be best off with our own implementation here, since it would be ideal to tailor it very closely to the rest of the API. |
This request type is quite common for file upload APIs, so it would be nice to have some ergonomic helpers to construct them. Support of streaming uploads is essential.
Examples of multipart APIs:
The text was updated successfully, but these errors were encountered: