Header Implementations "Content-Disposition"

# by analogy
# python as an example
output = io.StringIO()
fle = csv.writer(output)
content = output.getvalue()
headers = {"Content-Disposition": "attachment;filename=as.csv"}
return Response(content, headers=headers)

// axum rust as an example
use axum::{
    extract::{State},
    response::{IntoResponse},
    body::Body,
    http::{
        Response, StatusCode,
    },
};
use csv::Writer;

pub async fn import_users(State(pool): State<Pool>) -> impl IntoResponse {
let mut conn = pool.get().await.unwrap();
use schema::users::dsl::*;
let data = users
    .select(ListUser::as_select())
    .load(&mut conn)
    .await.unwrap();
let mut wtr = Writer::from_writer(vec![]);
for pat in data {
    wtr.serialize(pat).unwrap();
}
wtr.flush().unwrap();
return Response::builder()
    .status(StatusCode::OK)
    .header("Location", "/")
    .header("Content-Disposition", "attachment;filename=or.csv")
    .body(Body::from("out"))
    .unwrap()
}

.body(Body::from("..?"))
I've been looking for solutions for a long time, I hope for help.

error[E0277]: the trait bound `Body: From<Writer<Vec<u8>>>` is not satisfied
  --> src\import_export\handlers.rs:51:15
   |
51 |         .body(Body::from(wtr))
   |               ^^^^ the trait `From<Writer<Vec<u8>>>` is not implemented for `Body`
   |
   = help: the following other types implement trait `From<T>`:
             <Body as From<axum::body::Bytes>>
             <Body as From<Cow<'static, str>>>
             <Body as From<Cow<'static, [u8]>>>
             <Body as From<Vec<u8>>>
             <Body as From<std::string::String>>
             <Body as From<&'static str>>
             <Body as From<&'static [u8]>>
             <Body as From<()>>

I'm guessing you're trying to do something like

    .body(wtr.into_inner().unwrap())
1 Like

.body(Body::from(wtr.into_inner().unwrap()))
Thank you sincerely

1 Like