[−][src]Struct rocket::http::Accept
The HTTP Accept header.
An Accept
header is composed of zero or more media types, each of which
may have an optional quality value (a QMediaType
). The header is sent by an HTTP client to
describe the formats it accepts as well as the order in which it prefers
different formats.
Usage
The Accept header of an incoming request can be retrieved via the
Request::accept
method. The preferred
method can be used to retrieve
the client's preferred media type.
An Accept
type with a single, common media type can be easily constructed
via provided associated constants.
Example
Construct an Accept
header with a single application/json
media type:
use rocket::http::Accept; let accept_json = Accept::JSON;
Header
Accept
implements Into<Header>
. As such, it can be used in any context
where an Into<Header>
is expected:
use rocket::http::Accept; use rocket::response::Response; let response = Response::build().header(Accept::JSON).finalize();
Methods
impl Accept
[src]
impl Accept
pub fn new<T: IntoCollection<QMediaType>>(items: T) -> Accept
[src]
pub fn new<T: IntoCollection<QMediaType>>(items: T) -> Accept
Constructs a new Accept
header from one or more media types.
The items
parameter may be of type QMediaType
, &[QMediaType]
, or
Vec<QMediaType>
. To prevent additional allocations, prefer to provide
inputs of type QMediaType
and Vec<QMediaType>
.
Example
use rocket::http::{QMediaType, MediaType, Accept}; // Construct an `Accept` via a `Vec<QMediaType>`. let json_then_html = vec![MediaType::JSON.into(), MediaType::HTML.into()]; let accept = Accept::new(json_then_html); assert_eq!(accept.preferred().media_type(), &MediaType::JSON); // Construct an `Accept` via an `&[QMediaType]`. let accept = Accept::new(&[MediaType::JSON.into(), MediaType::HTML.into()]); assert_eq!(accept.preferred().media_type(), &MediaType::JSON); // Construct an `Accept` via a `QMediaType`. let accept = Accept::new(QMediaType(MediaType::JSON, None)); assert_eq!(accept.preferred().media_type(), &MediaType::JSON);
pub fn preferred(&self) -> &QMediaType
[src]
pub fn preferred(&self) -> &QMediaType
Retrieve the client's preferred media type. This method follows RFC
7231 5.3.2. If the list of media types is empty, this method returns a
media type of any with no quality value: (*/*
).
Example
use rocket::http::{QMediaType, MediaType, Accept}; let media_types = vec![ QMediaType(MediaType::JSON, Some(0.3)), QMediaType(MediaType::HTML, Some(0.9)) ]; let accept = Accept::new(media_types); assert_eq!(accept.preferred().media_type(), &MediaType::HTML);
pub fn first(&self) -> Option<&QMediaType>
[src]
pub fn first(&self) -> Option<&QMediaType>
Retrieve the first media type in self
, if any.
Example
use rocket::http::{QMediaType, MediaType, Accept}; let accept = Accept::new(QMediaType(MediaType::XML, None)); assert_eq!(accept.first(), Some(&MediaType::XML.into()));
pub fn iter<'a>(
&'a self
) -> impl Iterator<Item = &'a QMediaType> + 'a
[src]
pub fn iter<'a>(
&'a self
) -> impl Iterator<Item = &'a QMediaType> + 'a
Returns an iterator over all of the (quality) media types in self
.
Media types are returned in the order in which they appear in the
header.
Example
use rocket::http::{QMediaType, MediaType, Accept}; let qmedia_types = vec![ QMediaType(MediaType::JSON, Some(0.3)), QMediaType(MediaType::HTML, Some(0.9)) ]; let accept = Accept::new(qmedia_types.clone()); let mut iter = accept.iter(); assert_eq!(iter.next(), Some(&qmedia_types[0])); assert_eq!(iter.next(), Some(&qmedia_types[1])); assert_eq!(iter.next(), None);
pub fn media_types<'a>(
&'a self
) -> impl Iterator<Item = &'a MediaType> + 'a
[src]
pub fn media_types<'a>(
&'a self
) -> impl Iterator<Item = &'a MediaType> + 'a
Returns an iterator over all of the (bare) media types in self
. Media
types are returned in the order in which they appear in the header.
Example
use rocket::http::{QMediaType, MediaType, Accept}; let qmedia_types = vec![ QMediaType(MediaType::JSON, Some(0.3)), QMediaType(MediaType::HTML, Some(0.9)) ]; let accept = Accept::new(qmedia_types.clone()); let mut iter = accept.media_types(); assert_eq!(iter.next(), Some(qmedia_types[0].media_type())); assert_eq!(iter.next(), Some(qmedia_types[1].media_type())); assert_eq!(iter.next(), None);
pub const Any: Accept
[src]
An Accept
header with the single media type for
any media type
:
/
pub const Binary: Accept
[src]
An Accept
header with the single media type for
binary data
:
application
/
octet-stream
pub const HTML: Accept
[src]
An Accept
header with the single media type for
HTML
:
text
/
html
pub const Plain: Accept
[src]
An Accept
header with the single media type for
plain text
:
text
/
plain
pub const JSON: Accept
[src]
An Accept
header with the single media type for
JSON
:
application
/
json
pub const MsgPack: Accept
[src]
An Accept
header with the single media type for
MessagePack
:
application
/
msgpack
pub const Form: Accept
[src]
An Accept
header with the single media type for
forms
:
application
/
x-www-form-urlencoded
pub const JavaScript: Accept
[src]
An Accept
header with the single media type for
JavaScript
:
application
/
javascript
pub const CSS: Accept
[src]
An Accept
header with the single media type for
CSS
:
text
/
css
pub const FormData: Accept
[src]
An Accept
header with the single media type for
multipart form data
:
multipart
/
form-data
pub const XML: Accept
[src]
An Accept
header with the single media type for
XML
:
text
/
xml
pub const CSV: Accept
[src]
An Accept
header with the single media type for
CSV
:
text
/
csv
pub const PNG: Accept
[src]
An Accept
header with the single media type for
PNG
:
image
/
png
pub const GIF: Accept
[src]
An Accept
header with the single media type for
GIF
:
image
/
gif
pub const BMP: Accept
[src]
An Accept
header with the single media type for
BMP
:
image
/
bmp
pub const JPEG: Accept
[src]
An Accept
header with the single media type for
JPEG
:
image
/
jpeg
pub const WEBP: Accept
[src]
An Accept
header with the single media type for
WEBP
:
image
/
webp
pub const SVG: Accept
[src]
An Accept
header with the single media type for
SVG
:
image
/
svg+xml
pub const WEBM: Accept
[src]
An Accept
header with the single media type for
WEBM
:
video
/
webm
pub const OGG: Accept
[src]
An Accept
header with the single media type for
OGG
:
video
/
ogg
pub const WAV: Accept
[src]
An Accept
header with the single media type for
WAV
:
audio
/
wav
pub const PDF: Accept
[src]
An Accept
header with the single media type for
PDF
:
application
/
pdf
pub const TTF: Accept
[src]
An Accept
header with the single media type for
TTF
:
application
/
font-sfnt
pub const OTF: Accept
[src]
An Accept
header with the single media type for
OTF
:
application
/
font-sfnt
pub const WOFF: Accept
[src]
An Accept
header with the single media type for
WOFF
:
application
/
font-woff
pub const WOFF2: Accept
[src]
An Accept
header with the single media type for
WOFF2
:
font
/
woff2
pub const WASM: Accept
[src]
An Accept
header with the single media type for
WASM
:
application
/
wasm
pub const JsonApi: Accept
[src]
An Accept
header with the single media type for
JSON API
:
application
/
vnd.api+json
Trait Implementations
impl<'a, 'r> FromRequest<'a, 'r> for &'a Accept
[src]
impl<'a, 'r> FromRequest<'a, 'r> for &'a Accept
type Error = ()
The associated error to be returned if derivation fails.
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>
[src]
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>
impl PartialEq<Accept> for Accept
[src]
impl PartialEq<Accept> for Accept
impl Clone for Accept
[src]
impl Clone for Accept
fn clone(&self) -> Accept
[src]
fn clone(&self) -> Accept
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<T: IntoCollection<MediaType>> From<T> for Accept
[src]
impl<T: IntoCollection<MediaType>> From<T> for Accept
impl Into<Header<'static>> for Accept
[src]
impl Into<Header<'static>> for Accept
Creates a new Header
with name Accept
and the value set to the HTTP
rendering of this Accept
header.
impl Display for Accept
[src]
impl Display for Accept
impl Debug for Accept
[src]
impl Debug for Accept
impl FromStr for Accept
[src]
impl FromStr for Accept
Auto Trait Implementations
Blanket Implementations
impl<T> ToString for T where
T: Display + ?Sized,
[src]
impl<T> ToString for T where
T: Display + ?Sized,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
impl<T> Typeable for T where
T: Any,
[src]
impl<T> Typeable for T where
T: Any,