对于 axum,如何为处理程序设置标头

The*_*tix 2 http-headers rust response-headers rust-axum

我似乎找不到如何为响应设置标头。

我一直在寻找如何做到这一点,但还没有找到一个简单的方法来做到这一点。

特别强调标content-type头,如何从响应处理程序设置标准标头和自定义标头,记住我已经可以做到thing.into_response()

at5*_*321 6

以下是如何在处理程序中设置自定义响应标头的示例:

use axum::http::HeaderMap;
use axum::response::IntoResponse;

async fn my_handler() -> impl IntoResponse {
    let mut headers = HeaderMap::new();
    headers.insert("x-my-hdr", "abc".parse().unwrap());
    (headers, "It works!")
}
Run Code Online (Sandbox Code Playgroud)

我已经使用自定义和标准标头(例如Content-Type)测试了上述内容,它似乎在这两种情况下都有效。