pub struct Header<T> {
pub value: T,
pub name: String,
}Expand description
Header extractor for individual HTTP headers.
Extracts a single header value by name from the request. The header name
is derived from the generic type’s name, converting from snake_case to
Header-Case (e.g., x_request_id -> X-Request-Id).
For required headers, extraction failure returns 400 Bad Request.
Use Option<Header<T>> for optional headers.
§Example
ⓘ
use fastapi_core::extract::Header;
// Extract Authorization header (required)
async fn protected(auth: Header<String>) -> impl IntoResponse {
format!("Authorized with: {}", auth.0)
}
// Extract optional header
async fn optional_header(trace_id: Option<Header<String>>) -> impl IntoResponse {
match trace_id {
Some(Header(id)) => format!("Trace: {id}"),
None => "No trace".into(),
}
}Fields§
§value: TThe extracted header value.
name: StringThe original header name used for extraction.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Header<T>where
T: Freeze,
impl<T> RefUnwindSafe for Header<T>where
T: RefUnwindSafe,
impl<T> Send for Header<T>where
T: Send,
impl<T> Sync for Header<T>where
T: Sync,
impl<T> Unpin for Header<T>where
T: Unpin,
impl<T> UnwindSafe for Header<T>where
T: UnwindSafe,
Blanket Implementations§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).