pub struct AcceptHeader {
pub items: Vec<AcceptItem>,
}Expand description
Parsed Accept header with quality-ordered media types.
This extractor parses the Accept header and provides the list of acceptable media types sorted by quality (highest first).
§Example
ⓘ
use fastapi_core::AcceptHeader;
async fn handler(accept: AcceptHeader) -> impl IntoResponse {
if accept.prefers("application/json") {
Json(data).into_response()
} else if accept.prefers("text/html") {
Html(template).into_response()
} else {
// Default to JSON
Json(data).into_response()
}
}Fields§
§items: Vec<AcceptItem>Media types sorted by quality (highest first).
Implementations§
Source§impl AcceptHeader
impl AcceptHeader
Sourcepub fn prefers(&self, media_type: &str) -> bool
pub fn prefers(&self, media_type: &str) -> bool
Check if a media type is the preferred type.
Returns true if the given media type matches the highest-quality acceptable type.
Sourcepub fn quality_of(&self, media_type: &str) -> f32
pub fn quality_of(&self, media_type: &str) -> f32
Get the quality value for a specific media type.
Returns 0.0 if not acceptable, or the quality value if acceptable.
Sourcepub fn negotiate<'a>(&self, available: &[&'a str]) -> Option<&'a str>
pub fn negotiate<'a>(&self, available: &[&'a str]) -> Option<&'a str>
Negotiate the best media type from a list of available types.
Returns the first available type that is acceptable, preferring higher quality matches.
§Example
ⓘ
let accept = AcceptHeader::parse("text/html, application/json;q=0.9");
let available = ["application/json", "text/html", "text/plain"];
let best = accept.negotiate(&available);
assert_eq!(best, Some("text/html"));Trait Implementations§
Source§impl Clone for AcceptHeader
impl Clone for AcceptHeader
Source§fn clone(&self) -> AcceptHeader
fn clone(&self) -> AcceptHeader
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AcceptHeader
impl Debug for AcceptHeader
Source§impl Default for AcceptHeader
impl Default for AcceptHeader
Source§impl FromRequest for AcceptHeader
impl FromRequest for AcceptHeader
Source§type Error = Infallible
type Error = Infallible
Error type when extraction fails.
Source§async fn from_request(
_ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( _ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
Auto Trait Implementations§
impl Freeze for AcceptHeader
impl RefUnwindSafe for AcceptHeader
impl Send for AcceptHeader
impl Sync for AcceptHeader
impl Unpin for AcceptHeader
impl UnwindSafe for AcceptHeader
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).