Struct rocket::http::Accept
[−]
[src]
pub struct 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]
fn new<T: IntoCollection<QMediaType>>(items: T) -> Accept[src]
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);
fn preferred(&self) -> &QMediaType[src]
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);
fn first(&self) -> Option<&QMediaType>[src]
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()));
fn iter<'a>(&'a self) -> impl Iterator<Item = &'a QMediaType> + 'a[src]
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);
fn media_types<'a>(&'a self) -> impl Iterator<Item = &'a MediaType> + 'a[src]
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);
const Any: Accept
Any: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::Any, None)]))
An Accept header with the single media type for
:
/
const Binary: Accept
Binary: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::Binary, None)]))
An Accept header with the single media type for
:
/
const HTML: Accept
HTML: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::HTML, None)]))
An Accept header with the single media type for
:
/
const Plain: Accept
Plain: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::Plain, None)]))
An Accept header with the single media type for
:
/
const JSON: Accept
JSON: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::JSON, None)]))
An Accept header with the single media type for
:
/
const MsgPack: Accept
MsgPack: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::MsgPack, None)]))
An Accept header with the single media type for
:
/
const Form: Accept
Form: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::Form, None)]))
An Accept header with the single media type for
:
/
const JavaScript: Accept
JavaScript: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::JavaScript, None)]))
An Accept header with the single media type for
:
/
const CSS: Accept
CSS: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::CSS, None)]))
An Accept header with the single media type for
:
/
const FormData: Accept
FormData: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::FormData, None)]))
An Accept header with the single media type for
:
/
const XML: Accept
XML: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::XML, None)]))
An Accept header with the single media type for
:
/
const CSV: Accept
CSV: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::CSV, None)]))
An Accept header with the single media type for
:
/
const PNG: Accept
PNG: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::PNG, None)]))
An Accept header with the single media type for
:
/
const GIF: Accept
GIF: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::GIF, None)]))
An Accept header with the single media type for
:
/
const BMP: Accept
BMP: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::BMP, None)]))
An Accept header with the single media type for
:
/
const JPEG: Accept
JPEG: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::JPEG, None)]))
An Accept header with the single media type for
:
/
const WEBP: Accept
WEBP: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::WEBP, None)]))
An Accept header with the single media type for
:
/
const SVG: Accept
SVG: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::SVG, None)]))
An Accept header with the single media type for
:
/
const PDF: Accept
PDF: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::PDF, None)]))
An Accept header with the single media type for
:
/
const TTF: Accept
TTF: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::TTF, None)]))
An Accept header with the single media type for
:
/
const OTF: Accept
OTF: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::OTF, None)]))
An Accept header with the single media type for
:
/
const WOFF: Accept
WOFF: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::WOFF, None)]))
An Accept header with the single media type for
:
/
const WOFF2: Accept
WOFF2: Accept = Accept(AcceptParams::Static(&[QMediaType(<MediaType>::WOFF2, None)]))
An Accept header with the single media type for
:
/
Trait Implementations
impl Debug for Accept[src]
impl Clone for Accept[src]
fn clone(&self) -> Accept[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
Performs copy-assignment from source. Read more
impl PartialEq for Accept[src]
fn eq(&self, __arg_0: &Accept) -> bool[src]
This method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, __arg_0: &Accept) -> bool[src]
This method tests for !=.
impl<T: IntoCollection<MediaType>> From<T> for Accept[src]
impl Display for Accept[src]
fn fmt(&self, f: &mut Formatter) -> Result[src]
Formats the value using the given formatter. Read more
impl FromStr for Accept[src]
type Err = String
The associated error which can be returned from parsing.
fn from_str(raw: &str) -> Result<Accept, String>[src]
Parses a string s to return a value of this type. Read more
impl Into<Header<'static>> for Accept[src]
Creates a new Header with name Accept and the value set to the HTTP
rendering of this Accept header.