I'm able to request a PNG file with reqwest which I can save to a file through copy()
.
I'd like to forward the image as a Rocket response. I don't know how to pass the response contents there.
I tried to use Content(ContentType::PNG, response)
but I can't figure out how to match a type that has implemented rocket::response::Responder
trait. Rocket can't use the Bytes
type returned by response.bytes()
.
#![feature(proc_macro_hygiene, decl_macro)]
extern crate reqwest;
#[macro_use]
extern crate rocket;
use anyhow::Result;
use reqwest::header::USER_AGENT;
use reqwest::Response;
use rocket::http::ContentType;
use rocket::response::content::Content;
#[get("/")]
fn myroute() -> Result<Content<String>, anyhow::Error> {
let client = reqwest::blocking::Client::new();
let response = client.get("https://www.rust-lang.org/static/images/rust-logo-blk.svg")
.header(USER_AGENT, "rust-reqwest")
.send()?;
Ok(Content(ContentType::PNG, response.bytes()))
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![myroute])
}
fn main() {
rocket().launch();
}
[dependencies]
anyhow = "1.0"
rocket = "0.4.2"
reqwest = { version = "0.10.4", features = ["blocking", "json"] }