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,
test::TestClient,
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 = TestClient::new(app).get("/").send().await;
resp.assert_status_is_ok();
resp.assert_header_all("MyHeader1", ["a", "b"]);
resp.assert_header_all("MyHeader2", ["b"]);
Implementations§
source§impl SetHeader
impl SetHeader
sourcepub fn overriding<K, V>(self, key: K, value: V) -> Self
pub fn overriding<K, V>(self, key: K, value: V) -> Self
Inserts a header to response.
If a previous value exists for the same header, it is removed and replaced with the new header value.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SetHeader
impl RefUnwindSafe for SetHeader
impl Send for SetHeader
impl Sync for SetHeader
impl Unpin for SetHeader
impl UnwindSafe for SetHeader
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.