Passing bytes to reqwest::multipart, Cow needed

I get the body of an HTTP request (binary response) like so:

let client = reqwest::blocking::Client::new();
let data = client.get("http://example.com").send().unwrap().bytes().unwrap();

Now I would like to build a reqwest::multipart::Part from this data, so I tried:

let part = reqwest::multipart::Part::bytes(data);

However, this gives me:

15  |     let part = reqwest::multipart::Part::bytes(data);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<bytes::bytes::Bytes>` is not implemented for `std::borrow::Cow<'static, [u8]>`

So, how do I get a Cow?

A Cow<'static, [u8]> can be constructed from either a &'static [u8] or a Vec<u8>. The former is only for compile time constants, so I think you'll need to provide the latter.

It seems weird that they use the Bytes library without accepting it everywhere. But regardless, you'll need to create a Vec<u8> from the Bytes in order to pass in. I think you can call .to_vec() on the Bytes to do this.

1 Like

Nit: That should be a &'static [u8]

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.