Struct poem::middleware::SetHeader
source · [−]pub struct SetHeader { /* private fields */ }
Expand description
Middleware for override/append headers to response.
Example
use poem::{
get, handler,
http::{HeaderValue, StatusCode},
middleware::SetHeader,
Endpoint, EndpointExt, Request, Route,
};
#[handler]
fn index() -> &'static str {
"hello"
}
let app = Route::new().at("/", get(index)).with(
SetHeader::new()
.appending("MyHeader1", "a")
.appending("MyHeader1", "b")
.overriding("MyHeader2", "a")
.overriding("MyHeader2", "b"),
);
let resp = app.call(Request::default()).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers()
.get_all("MyHeader1")
.iter()
.collect::<Vec<_>>(),
vec![HeaderValue::from_static("a"), HeaderValue::from_static("b")]
);
assert_eq!(
resp.headers()
.get_all("MyHeader2")
.iter()
.collect::<Vec<_>>(),
vec![HeaderValue::from_static("b")]
);
Implementations
pub fn overriding<K, V>(self, key: K, value: V) -> Self where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
pub fn overriding<K, V>(self, key: K, value: V) -> Self where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Inserts a header to response.
If a previous value exists for the same header, it is removed and replaced with the new header value.
pub fn appending<K, V>(self, key: K, value: V) -> Self where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
pub fn appending<K, V>(self, key: K, value: V) -> Self where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Appends a header to response.
If previous values exist, the header will have multiple values.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for SetHeader
impl UnwindSafe for SetHeader
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more