reqwest
reqwest copied to clipboard
Adding file function to async::multipart::Form
In the interest of feature parity between multipart::Form
and async::multipart::Form
, would a file
function similar to the existing one for multipart::Form
for async::multipart::Form
be in scope for this library? I'm thinking something using tokio::fs::File
. I have yet to explore implementing this but if there is interest I can start experimenting and open a PR.
Yes, I think this is especially easier in current master using tokio 0.2, since then tokio::fs
works in any executor.
I also at times wonder if it'd be feasible to combine the two multipart builders...
Good to hear! I haven't used tokio directly much before, but I'll start a branch this weekend time permitting.
I think combining the multipart builders into one is worthwhile. I haven't looked at the internals of the builders, but logically separating Forms/Parts and the Client that sends them completely seems like a good design move.
Is there any update for this? Thanks.
You can do this with a few bits and pieces as I have done in lorikeet:
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
let file_name = path
.file_name()
.map(|val| val.to_string_lossy().to_string())
.unwrap_or_default();
let file = File::open(&path)
.await?;
let reader = Body::wrap_stream(FramedRead::new(file, BytesCodec::new()));
form.part(field_name, Part::stream(reader).file_name(file_name))
It would be nice to have this method back inside reqwest though.
You can do this with a few bits and pieces as I have done in lorikeet:
use tokio::fs::File; use tokio_util::codec::{BytesCodec, FramedRead}; let file_name = path .file_name() .map(|val| val.to_string_lossy().to_string()) .unwrap_or_default(); let file = File::open(&path) .await?; let reader = Body::wrap_stream(FramedRead::new(file, BytesCodec::new())); form.part(field_name, Part::stream(reader).file_name(file_name))
It would be nice to have this method back inside reqwest though.
thx for the codes, it save my life. especially for the .file_name(file_name)