[][src]Struct rocket::http::Accept

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]

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);

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);

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()));

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);

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);

An Accept header with the single media type for any media type :

/

An Accept header with the single media type for binary data : application / octet-stream

An Accept header with the single media type for HTML : text / html

An Accept header with the single media type for plain text : text / plain

An Accept header with the single media type for JSON : application / json

An Accept header with the single media type for MessagePack : application / msgpack

An Accept header with the single media type for forms : application / x-www-form-urlencoded

An Accept header with the single media type for JavaScript : application / javascript

An Accept header with the single media type for CSS : text / css

An Accept header with the single media type for multipart form data : multipart / form-data

An Accept header with the single media type for XML : text / xml

An Accept header with the single media type for CSV : text / csv

An Accept header with the single media type for PNG : image / png

An Accept header with the single media type for GIF : image / gif

An Accept header with the single media type for BMP : image / bmp

An Accept header with the single media type for JPEG : image / jpeg

An Accept header with the single media type for WEBP : image / webp

An Accept header with the single media type for SVG : image / svg+xml

An Accept header with the single media type for WEBM : video / webm

An Accept header with the single media type for OGG : video / ogg

An Accept header with the single media type for WAV : audio / wav

An Accept header with the single media type for PDF : application / pdf

An Accept header with the single media type for TTF : application / font-sfnt

An Accept header with the single media type for OTF : application / font-sfnt

An Accept header with the single media type for WOFF : application / font-woff

An Accept header with the single media type for WOFF2 : font / woff2

An Accept header with the single media type for WASM : application / wasm

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]

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

impl<T: IntoCollection<MediaType>> From<T> for Accept
[src]

Performs the conversion.

impl Clone for Accept
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. 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.

Performs the conversion.

impl PartialEq<Accept> for Accept
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Display for Accept
[src]

Formats the value using the given formatter. Read more

impl Debug for Accept
[src]

Formats the value using the given formatter. Read more

impl FromStr for Accept
[src]

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Auto Trait Implementations

impl Send for Accept

impl Sync for Accept

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

Converts the given value to a String. Read more

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.