Using Reqwest to download an image and convert it to base64

Hello, I am having difficulties to understand the thing that I am doing wrong...

I want to be able to download an image and to convert it into a base64 image

I slightly modified the image-base64 crate and extend it to create a png_url_to_base64 function using reqwest and create a test for it the crate has already everything in it to test it was easier this way so you can look at the changes here:

The crate is using rustc_serialize in order to convert to base64, but I also tried the base64 crate and the data_encoding crate with the same results.

The test fails because the hex conversion does not match the png signature ( I assume that an hex version of a png file should always starts with "89504e47" )

Anyhow, even if I trick the function get_file_type to always returns "png", the base64 part is not as expected...

So I am thinking that I may need to perform some action on the output of reqwest..? ( Assuming that there are no bugs... )

If this may change something, I am running rustc 1.45.0-nightly under ubuntu 20.04

Thanks for reading and have a good day!

You are calling text on the returned body, which does a lossy utf-8 conversion using String::from_utf8_lossy. Use Response::bytes instead.

Thanks for the quick reply, it works!

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.