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
sourceimpl SetHeader
impl SetHeader
sourcepub 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.
sourcepub 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 Send for SetHeader
impl Sync for SetHeader
impl Unpin for SetHeader
impl UnwindSafe for SetHeader
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more