1

我可以使用 reqwest 请求一个 PNG 文件,我可以通过copy().

我想将图像作为火箭响应转发。我不知道如何在那里传递响应内容。

我尝试使用Content(ContentType::PNG, response),但不知道如何匹配已实现rocket::response::Responder特征的类型。Rocket 不能使用Bytes返回的类型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"] }
4

0 回答 0