0

As this answer explains, it's possible to use Body::wrap_stream(read_stream) to POST the contents of a file without first reading the entire contents into memory.

How can we do the same thing as part of a reqwest::multipart::Form? The following code fails with the error the trait `From<&mut dyn Stream<Item = std::result::Result<Vec<u8>, std::io::Error>>>` is not implemented for `Body` .

   let metadata_json = "{ \"file_owner\": \"bob smith\" }";
   let metadata_part = reqwest::multipart::Part::text(metadata_json);

   let read_stream : Stream<Item = std::io::Result<Vec<u8>> = my_file_stream;
   let stream_part = reqwest::multipart::Part::stream(read_stream);

   let multipart_form = reqwest::multipart::Form::new()
     .part("metadata", metadata_part)
     .part("file", stream_part);

I tried supplying an implementation of From that just calls Body::wrap_stream but it's forbidden since neither From nor Body is defined in my own code.

Eric
  • 11,392
  • 13
  • 57
  • 100

1 Answers1

0

I believe you need to use Body::wrap_stream like this:

let stream_part = reqwest::multipart::Part::stream(Body::wrap_stream(read_stream));
Enn Michael
  • 1,328
  • 14
  • 30
  • The stream is returning data from a C api that retrieves one buffer at a time. Is there a more idiomatic way to represent its data? – Eric Mar 17 '21 at 22:56
  • Ah, sorry. No, then it is completely fine. (You could also flatten the `Vec`s, I suppose, but if there's no particular reason to do that, then it's completely fine as it is.) I've edited my answer accordingly. – Enn Michael Mar 18 '21 at 01:23