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§
source§impl Accept
impl Accept
sourcepub fn new<T>(items: T) -> Acceptwhere
T: IntoCollection<QMediaType>,
pub fn new<T>(items: T) -> Acceptwhere T: IntoCollection<QMediaType>,
Constructs a new Accept
header from one or more media types.
The items
parameter may be of type QMediaType
, [QMediaType]
,
&[QMediaType]
or Vec<QMediaType>
. To prevent additional allocations,
prefer to provide inputs of type QMediaType
, [QMediaType]
, or
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(&self) -> impl Iterator<Item = &QMediaType>
pub fn iter(&self) -> impl Iterator<Item = &QMediaType>
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(&self) -> impl Iterator<Item = &MediaType>
pub fn media_types(&self) -> impl Iterator<Item = &MediaType>
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 Bytes: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::Bytes, None)))
pub const Bytes: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::Bytes, 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 Text: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::Text, None)))
pub const Text: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::Text, 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 OPF: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::OPF, None)))
pub const OPF: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::OPF, None)))
An Accept
header with the single media type for
OPF
:
application
/
oebps-package+xml
sourcepub const XHTML: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::XHTML, None)))
pub const XHTML: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::XHTML, None)))
An Accept
header with the single media type for
XHTML
:
application
/
xhtml+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 AVIF: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::AVIF, None)))
pub const AVIF: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::AVIF, None)))
An Accept
header with the single media type for
AVIF
:
image
/
avif
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
sourcepub const MP3: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::MP3, None)))
pub const MP3: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::MP3, None)))
An Accept
header with the single media type for
MPEG Audio
:
audio
/
mpeg
sourcepub const MP4: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::MP4, None)))
pub const MP4: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::MP4, None)))
An Accept
header with the single media type for
MPEG4 Video
:
video
/
mp4
sourcepub const ZIP: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::ZIP, None)))
pub const ZIP: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::ZIP, None)))
An Accept
header with the single media type for
ZIP archive
:
application
/
zip
sourcepub const CBZ: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::CBZ, None)))
pub const CBZ: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::CBZ, None)))
An Accept
header with the single media type for
Comic ZIP archive
:
application
/
vnd.comicbook+zip
sourcepub const CBR: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::CBR, None)))
pub const CBR: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::CBR, None)))
An Accept
header with the single media type for
Comic RAR compressed archive
:
application
/
vnd.comicbook-rar
sourcepub const RAR: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::RAR, None)))
pub const RAR: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::RAR, None)))
An Accept
header with the single media type for
RAR compressed archive
:
application
/
vnd.rar
sourcepub const EPUB: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::EPUB, None)))
pub const EPUB: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::EPUB, None)))
An Accept
header with the single media type for
EPUB
:
application
/
epub+zip
sourcepub const EventStream: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::EventStream, None)))
pub const EventStream: Accept = Accept(AcceptParams::Static(QMediaType(MediaType::EventStream, None)))
An Accept
header with the single media type for
SSE stream
:
text
/
event-stream
Trait Implementations§
source§impl From<Accept> for Header<'static>
impl From<Accept> for Header<'static>
Creates a new Header
with name Accept
and the value set to the HTTP
rendering of this Accept
header.