Struct rocket_http::Accept
source · [−]pub struct Accept(_);
Expand description
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();
Implementations
sourceimpl Accept
impl Accept
sourcepub fn new<T: IntoCollection<QMediaType>>(items: T) -> Accept
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);
sourcepub fn preferred(&self) -> &QMediaType
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);
sourcepub fn first(&self) -> Option<&QMediaType>
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()));
sourcepub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a QMediaType> + 'a
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);
sourcepub fn media_types<'a>(&'a self) -> impl Iterator<Item = &'a MediaType> + 'a
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);
sourcepub const Any: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Any, None)]))
pub const Any: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Any, None)]))
An Accept
header with the single media type for
any media type
:
/
sourcepub const Binary: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Binary, None)]))
pub const Binary: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Binary, None)]))
An Accept
header with the single media type for
binary data
:
application
/
octet-stream
sourcepub const HTML: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::HTML, None)]))
pub const HTML: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::HTML, None)]))
An Accept
header with the single media type for
HTML
:
text
/
html
sourcepub const Plain: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Plain, None)]))
pub const Plain: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Plain, None)]))
An Accept
header with the single media type for
plain text
:
text
/
plain
sourcepub const JSON: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JSON, None)]))
pub const JSON: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JSON, None)]))
An Accept
header with the single media type for
JSON
:
application
/
json
sourcepub const MsgPack: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MsgPack, None)]))
pub const MsgPack: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MsgPack, None)]))
An Accept
header with the single media type for
MsgPack
:
application
/
msgpack
sourcepub const Form: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Form, None)]))
pub const Form: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Form, None)]))
An Accept
header with the single media type for
forms
:
application
/
x-www-form-urlencoded
sourcepub const JavaScript: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JavaScript, None)]))
pub const JavaScript: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JavaScript, None)]))
An Accept
header with the single media type for
JavaScript
:
application
/
javascript
sourcepub const CSS: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::CSS, None)]))
pub const CSS: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::CSS, None)]))
An Accept
header with the single media type for
CSS
:
text
/
css
sourcepub const FormData: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::FormData, None)]))
pub const FormData: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::FormData, None)]))
An Accept
header with the single media type for
multipart form data
:
multipart
/
form-data
sourcepub const XML: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::XML, None)]))
pub const XML: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::XML, None)]))
An Accept
header with the single media type for
XML
:
text
/
xml
sourcepub const CSV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::CSV, None)]))
pub const CSV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::CSV, None)]))
An Accept
header with the single media type for
CSV
:
text
/
csv
sourcepub const PNG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::PNG, None)]))
pub const PNG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::PNG, None)]))
An Accept
header with the single media type for
PNG
:
image
/
png
sourcepub const GIF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::GIF, None)]))
pub const GIF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::GIF, None)]))
An Accept
header with the single media type for
GIF
:
image
/
gif
sourcepub const BMP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::BMP, None)]))
pub const BMP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::BMP, None)]))
An Accept
header with the single media type for
BMP
:
image
/
bmp
sourcepub const JPEG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JPEG, None)]))
pub const JPEG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JPEG, None)]))
An Accept
header with the single media type for
JPEG
:
image
/
jpeg
sourcepub const WEBP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBP, None)]))
pub const WEBP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBP, None)]))
An Accept
header with the single media type for
WEBP
:
image
/
webp
sourcepub const SVG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::SVG, None)]))
pub const SVG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::SVG, None)]))
An Accept
header with the single media type for
SVG
:
image
/
svg+xml
sourcepub const Icon: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Icon, None)]))
pub const Icon: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Icon, None)]))
An Accept
header with the single media type for
Icon
:
image
/
x-icon
sourcepub const WEBM: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBM, None)]))
pub const WEBM: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBM, None)]))
An Accept
header with the single media type for
WEBM
:
video
/
webm
sourcepub const WEBA: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBA, None)]))
pub const WEBA: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WEBA, None)]))
An Accept
header with the single media type for
WEBM Audio
:
audio
/
webm
sourcepub const OGG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::OGG, None)]))
pub const OGG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::OGG, None)]))
An Accept
header with the single media type for
OGG Video
:
video
/
ogg
sourcepub const FLAC: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::FLAC, None)]))
pub const FLAC: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::FLAC, None)]))
An Accept
header with the single media type for
FLAC
:
audio
/
flac
sourcepub const WAV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WAV, None)]))
pub const WAV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WAV, None)]))
An Accept
header with the single media type for
WAV
:
audio
/
wav
sourcepub const PDF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::PDF, None)]))
pub const PDF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::PDF, None)]))
An Accept
header with the single media type for
PDF
:
application
/
pdf
sourcepub const TTF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TTF, None)]))
pub const TTF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TTF, None)]))
An Accept
header with the single media type for
TTF
:
application
/
font-sfnt
sourcepub const OTF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::OTF, None)]))
pub const OTF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::OTF, None)]))
An Accept
header with the single media type for
OTF
:
application
/
font-sfnt
sourcepub const WOFF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WOFF, None)]))
pub const WOFF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WOFF, None)]))
An Accept
header with the single media type for
WOFF
:
application
/
font-woff
sourcepub const WOFF2: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WOFF2, None)]))
pub const WOFF2: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WOFF2, None)]))
An Accept
header with the single media type for
WOFF2
:
font
/
woff2
sourcepub const JsonApi: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JsonApi, None)]))
pub const JsonApi: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::JsonApi, None)]))
An Accept
header with the single media type for
JSON API
:
application
/
vnd.api+json
sourcepub const WASM: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WASM, None)]))
pub const WASM: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::WASM, None)]))
An Accept
header with the single media type for
WASM
:
application
/
wasm
sourcepub const TIFF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TIFF, None)]))
pub const TIFF: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TIFF, None)]))
An Accept
header with the single media type for
TIFF
:
image
/
tiff
sourcepub const AAC: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::AAC, None)]))
pub const AAC: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::AAC, None)]))
An Accept
header with the single media type for
AAC Audio
:
audio
/
aac
sourcepub const Calendar: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Calendar, None)]))
pub const Calendar: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::Calendar, None)]))
An Accept
header with the single media type for
iCalendar
:
text
/
calendar
sourcepub const MPEG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MPEG, None)]))
pub const MPEG: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MPEG, None)]))
An Accept
header with the single media type for
MPEG Video
:
video
/
mpeg
sourcepub const TAR: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TAR, None)]))
pub const TAR: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::TAR, None)]))
An Accept
header with the single media type for
tape archive
:
application
/
x-tar
sourcepub const GZIP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::GZIP, None)]))
pub const GZIP: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::GZIP, None)]))
An Accept
header with the single media type for
gzipped binary
:
application
/
gzip
sourcepub const MOV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MOV, None)]))
pub const MOV: Accept = Accept(AcceptParams::Static(&[QMediaType(MediaType::MOV, None)]))
An Accept
header with the single media type for
quicktime video
:
video
/
quicktime
Trait Implementations
sourceimpl<T: IntoCollection<MediaType>> From<T> for Accept
impl<T: IntoCollection<MediaType>> From<T> for Accept
sourceimpl Into<Header<'static>> for Accept
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 StructuralPartialEq for Accept
Auto Trait Implementations
impl RefUnwindSafe for Accept
impl Send for Accept
impl Sync for Accept
impl Unpin for Accept
impl UnwindSafe for Accept
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more