You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the recommend way to set a mime type on a multipart file Part, given that it might fail? The fact that there is no try_mime or something means that I when build a Part and attempt to set its mime type, I then might need to completely rebuild it, e.g.:
let mut part = Part::bytes(bytes.clone()).file_name(filename.clone());
if let Some(mime_type) = mime_guess::from_path(filename.clone()).first_raw() {
match part.mime_str(mime_type) {
Ok(part2) => part = part2,
Err(_) => part = Part::bytes(bytes).file_name(filename),
}
}
form = form.part(key, part);
(To be clear: this code compiles, but I did not actually end up testing it. I found this disagreeable enough to scrap it entirely.)
All I want is something like:
form = form.part(key, Part::bytes(bytes).file_name(filename).set_mime_best_guess());
Where it assigns a mime type from the filename, only if it can find a valid one -- the reqwest code already uses mime_guess for mime anyway -- and if I should still do the guessing myself, then I would like the set_mime() fn to be pub, since it's already there anyway.
I'm happy to put up a PR implementing my idea, if given the blessing to do so. And, certainly, if there is a better recommended approach, I will gladly use that. Thank you!
The text was updated successfully, but these errors were encountered:
rogusdev
changed the title
How best to set mime type on file part?
How best to set mime type on file Part?
Apr 11, 2024
What is the recommend way to set a mime type on a multipart file
Part
, given that it might fail? The fact that there is notry_mime
or something means that I when build aPart
and attempt to set its mime type, I then might need to completely rebuild it, e.g.:(To be clear: this code compiles, but I did not actually end up testing it. I found this disagreeable enough to scrap it entirely.)
All I want is something like:
Where it assigns a mime type from the filename, only if it can find a valid one -- the reqwest code already uses
mime_guess
formime
anyway -- and if I should still do the guessing myself, then I would like theset_mime()
fn to be pub, since it's already there anyway.Relevant code sections:
reqwest/src/blocking/multipart.rs
Line 45 in 0720159
reqwest/src/blocking/multipart.rs
Line 224 in 0720159
reqwest/src/blocking/multipart.rs
Line 248 in 0720159
I'm happy to put up a PR implementing my idea, if given the blessing to do so. And, certainly, if there is a better recommended approach, I will gladly use that. Thank you!
The text was updated successfully, but these errors were encountered: