carllerche / tower-web

A fast, boilerplate free, web framework for Rust
MIT License
981 stars 51 forks source link

how to handle image/png to response ? #165

Closed goodclouddata closed 5 years ago

goodclouddata commented 5 years ago

I want to output the png format to web,How to do this now? set content_type:image/png. if use Result<http::Response<Vec> ,Error> type, tower-web-0.3.3/src/response/context.rs line : 107 WARN: no default serialization format associated with action

kornholi commented 5 years ago
fn foo(&self) -> Result<http::Response<bytes::Bytes>, Error> {
    let png_bytes: Vec<u8> = /* ... */;
    Ok(http::Response::builder()
        .status(200)
        .header(http::header::CONTENT_TYPE, "image/png")
        .body(png_bytes.into())?
    )
}
goodclouddata commented 5 years ago

@kornholl ,Thanks a lot!

carllerche commented 5 years ago

I wonder if there should be some built in types for this kind of stuff...

#[derive(Response)]
#[web(header(name = "content-type", value = "image/png"))]
pub struct Png {
   body: Bytes,
}