Struct rocket::http::ContentType[][src]

pub struct ContentType(pub MediaType);
Expand description

Representation of HTTP Content-Types.

Usage

ContentTypes should rarely be created directly. Instead, an associated constant should be used; one is declared for most commonly used content types.

Example

A Content-Type of text/html; charset=utf-8 can be instantiated via the HTML constant:

use rocket::http::ContentType;

let html = ContentType::HTML;

Header

ContentType implements Into<Header>. As such, it can be used in any context where an Into<Header> is expected:

use rocket::http::ContentType;
use rocket::response::Response;

let response = Response::build().header(ContentType::HTML).finalize();

Implementations

Creates a new ContentType with top-level type top and subtype sub. This should only be used to construct uncommon or custom content types. Use an associated constant for everything else.

Example

Create a custom application/x-person content type:

use rocket::http::ContentType;

let custom = ContentType::new("application", "x-person");
assert_eq!(custom.top(), "application");
assert_eq!(custom.sub(), "x-person");

Flexibly parses name into a ContentType . The parse is flexible because, in addition to stricly correct content types, it recognizes the following shorthands:

  • “any” - ContentType::Any
  • “binary” - ContentType::Binary
  • “bytes” - ContentType::Bytes
  • “html” - ContentType::HTML
  • “plain” - ContentType::Plain
  • “text” - ContentType::Text
  • “json” - ContentType::JSON
  • “msgpack” - ContentType::MsgPack
  • “form” - ContentType::Form
  • “js” - ContentType::JavaScript
  • “css” - ContentType::CSS
  • “multipart” - ContentType::FormData
  • “xml” - ContentType::XML
  • “pdf” - ContentType::PDF For regular parsing, use the ContentType::from_str() method.

Example

Using a shorthand:

use rocket::http::ContentType;

let html = ContentType::parse_flexible("html");
assert_eq!(html, Some(ContentType::HTML));

let json = ContentType::parse_flexible("json");
assert_eq!(json, Some(ContentType::JSON));

Using the full content-type:

use rocket::http::ContentType;

let html = ContentType::parse_flexible("text/html; charset=utf-8");
assert_eq!(html, Some(ContentType::HTML));

let json = ContentType::parse_flexible("application/json");
assert_eq!(json, Some(ContentType::JSON));

let custom = ContentType::parse_flexible("application/x+custom");
assert_eq!(custom, Some(ContentType::new("application", "x+custom")));

An unrecognized content-type:

use rocket::http::ContentType;

let foo = ContentType::parse_flexible("foo");
assert_eq!(foo, None);

let bar = ContentType::parse_flexible("foo/bar/baz");
assert_eq!(bar, None);

Returns the Content-Type associated with the extension ext . Not all extensions are recognized. If an extensions is not recognized, None is returned. The currently recognized extensions are:

  • txt - ContentType::Plain
  • html - ContentType::HTML
  • htm - ContentType::HTML
  • xml - ContentType::XML
  • csv - ContentType::CSV
  • js - ContentType::JavaScript
  • css - ContentType::CSS
  • json - ContentType::JSON
  • png - ContentType::PNG
  • gif - ContentType::GIF
  • bmp - ContentType::BMP
  • jpeg - ContentType::JPEG
  • jpg - ContentType::JPEG
  • webp - ContentType::WEBP
  • avif - ContentType::AVIF
  • svg - ContentType::SVG
  • ico - ContentType::Icon
  • flac - ContentType::FLAC
  • wav - ContentType::WAV
  • webm - ContentType::WEBM
  • weba - ContentType::WEBA
  • ogg - ContentType::OGG
  • ogv - ContentType::OGG
  • pdf - ContentType::PDF
  • ttf - ContentType::TTF
  • otf - ContentType::OTF
  • woff - ContentType::WOFF
  • woff2 - ContentType::WOFF2
  • mp4 - ContentType::MP4
  • mpeg4 - ContentType::MP4
  • wasm - ContentType::WASM
  • aac - ContentType::AAC
  • ics - ContentType::Calendar
  • bin - ContentType::Binary
  • mpg - ContentType::MPEG
  • mpeg - ContentType::MPEG
  • tar - ContentType::TAR
  • gz - ContentType::GZIP
  • tif - ContentType::TIFF
  • tiff - ContentType::TIFF
  • mov - ContentType::MOV
  • zip - ContentType::ZIP

This list is likely to grow. Extensions are matched case-insensitively.

Example

Recognized content types:

use rocket::http::ContentType;

let xml = ContentType::from_extension("xml");
assert_eq!(xml, Some(ContentType::XML));

let xml = ContentType::from_extension("XML");
assert_eq!(xml, Some(ContentType::XML));

An unrecognized content type:

use rocket::http::ContentType;

let foo = ContentType::from_extension("foo");
assert!(foo.is_none());

Creates a new ContentType with top-level type top, subtype sub, and parameters ps. This should only be used to construct uncommon or custom content types. Use an associated constant for everything else.

Example

Create a custom application/x-id; id=1 content type:

use rocket::http::ContentType;

let id = ContentType::with_params("application", "x-id", ("id", "1"));
assert_eq!(id.to_string(), "application/x-id; id=1".to_string());

Create a custom text/person; name=bob; weight=175 content type:

use rocket::http::ContentType;

let params = vec![("name", "bob"), ("ref", "2382")];
let mt = ContentType::with_params("text", "person", params);
assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string());

Borrows the inner MediaType of self.

Example

use rocket::http::{ContentType, MediaType};

let http = ContentType::HTML;
let media_type = http.media_type();

Returns the most common file extension associated with the Content-Type self if it is known. Otherwise, returns None . The currently recognized extensions are identical to those in ContentType::from_extension() with the most common extension being the first extension appearing in the list for a given Content-Type .

Example

Known extension:

use rocket::http::ContentType;

assert_eq!(ContentType::JSON.extension().unwrap(), "json");
assert_eq!(ContentType::JPEG.extension().unwrap(), "jpeg");
assert_eq!(ContentType::JPEG.extension().unwrap(), "JPEG");
assert_eq!(ContentType::PDF.extension().unwrap(), "pdf");

An unknown extension:

use rocket::http::ContentType;

let foo = ContentType::new("foo", "bar");
assert!(foo.extension().is_none());

Content Type for any media type: */*.

Content Type for binary data: application/octet-stream.

Content Type for binary data: application/octet-stream.

Content Type for HTML: text/html; charset=utf-8.

Content Type for plain text: text/plain; charset=utf-8.

Content Type for plain text: text/plain; charset=utf-8.

Content Type for JSON: application/json.

Content Type for MsgPack: application/msgpack.

Content Type for forms: application/x-www-form-urlencoded.

Content Type for JavaScript: application/javascript.

Content Type for CSS: text/css; charset=utf-8.

Content Type for multipart form data: multipart/form-data.

Content Type for XML: text/xml; charset=utf-8.

Content Type for CSV: text/csv; charset=utf-8.

Content Type for PNG: image/png.

Content Type for GIF: image/gif.

Content Type for BMP: image/bmp.

Content Type for JPEG: image/jpeg.

Content Type for WEBP: image/webp.

Content Type for AVIF: image/avif.

Content Type for SVG: image/svg+xml.

Content Type for Icon: image/x-icon.

Content Type for WEBM: video/webm.

Content Type for WEBM Audio: audio/webm.

Content Type for OGG Video: video/ogg.

Content Type for FLAC: audio/flac.

Content Type for WAV: audio/wav.

Content Type for PDF: application/pdf.

Content Type for TTF: application/font-sfnt.

Content Type for OTF: application/font-sfnt.

Content Type for WOFF: application/font-woff.

Content Type for WOFF2: font/woff2.

Content Type for JSON API: application/vnd.api+json.

Content Type for WASM: application/wasm.

Content Type for TIFF: image/tiff.

Content Type for AAC Audio: audio/aac.

Content Type for iCalendar: text/calendar.

Content Type for MPEG Video: video/mpeg.

Content Type for tape archive: application/x-tar.

Content Type for gzipped binary: application/gzip.

Content Type for quicktime video: video/quicktime.

Content Type for MPEG4 Video: video/mp4.

Content Type for ZIP archive: application/zip.

Content Type for SSE stream: text/event-stream.

Methods from Deref<Target = MediaType>

Returns the top-level type for this media type. The return type, UncasedStr, has caseless equality comparison and hashing.

Example

use rocket::http::MediaType;

let plain = MediaType::Plain;
assert_eq!(plain.top(), "text");
assert_eq!(plain.top(), "TEXT");
assert_eq!(plain.top(), "Text");

Returns the subtype for this media type. The return type, UncasedStr, has caseless equality comparison and hashing.

Example

use rocket::http::MediaType;

let plain = MediaType::Plain;
assert_eq!(plain.sub(), "plain");
assert_eq!(plain.sub(), "PlaIN");
assert_eq!(plain.sub(), "pLaIn");

Returns a u8 representing how specific the top-level type and subtype of this media type are.

The return value is either 0, 1, or 2, where 2 is the most specific. A 0 is returned when both the top and sublevel types are *. A 1 is returned when only one of the top or sublevel types is *, and a 2 is returned when neither the top or sublevel types are *.

Example

use rocket::http::MediaType;

let mt = MediaType::Plain;
assert_eq!(mt.specificity(), 2);

let mt = MediaType::new("text", "*");
assert_eq!(mt.specificity(), 1);

let mt = MediaType::Any;
assert_eq!(mt.specificity(), 0);

Compares self with other and returns true if self and other are exactly equal to each other, including with respect to their parameters.

This is different from the PartialEq implementation in that it considers parameters. If PartialEq returns false, this function is guaranteed to return false. Similarly, if this function returns true, PartialEq is guaranteed to return true. However, if PartialEq returns true, this function may or may not return true.

Example

use rocket::http::MediaType;

let plain = MediaType::Plain;
let plain2 = MediaType::with_params("text", "plain", ("charset", "utf-8"));
let just_plain = MediaType::new("text", "plain");

// The `PartialEq` implementation doesn't consider parameters.
assert!(plain == just_plain);
assert!(just_plain == plain2);
assert!(plain == plain2);

// While `exact_eq` does.
assert!(!plain.exact_eq(&just_plain));
assert!(!plain2.exact_eq(&just_plain));
assert!(plain.exact_eq(&plain2));

Returns an iterator over the (key, value) pairs of the media type’s parameter list. The iterator will be empty if the media type has no parameters.

Example

The MediaType::Plain type has one parameter: charset=utf-8:

use rocket::http::MediaType;

let plain = MediaType::Plain;
let (key, val) = plain.params().next().unwrap();
assert_eq!(key, "charset");
assert_eq!(val, "utf-8");

The MediaType::PNG type has no parameters:

use rocket::http::MediaType;

let png = MediaType::PNG;
assert_eq!(png.params().count(), 0);

Returns the first parameter with name name, if there is any.

Returns the most common file extension associated with the Media-Type self if it is known. Otherwise, returns None . The currently recognized extensions are identical to those in MediaType::from_extension() with the most common extension being the first extension appearing in the list for a given Media-Type .

Example

Known extension:

use rocket::http::MediaType;

assert_eq!(MediaType::JSON.extension().unwrap(), "json");
assert_eq!(MediaType::JPEG.extension().unwrap(), "jpeg");
assert_eq!(MediaType::JPEG.extension().unwrap(), "JPEG");
assert_eq!(MediaType::PDF.extension().unwrap(), "pdf");

An unknown extension:

use rocket::http::MediaType;

let foo = MediaType::new("foo", "bar");
assert!(foo.extension().is_none());

Returns true if this MediaType is known to Rocket. In other words, returns true if there is an associated constant for self.

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Any .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Binary .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Bytes .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::HTML .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Plain .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Text .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::JSON .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::MsgPack .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Form .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::JavaScript .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::CSS .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::FormData .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::XML .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::CSV .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::PNG .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::GIF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::BMP .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::JPEG .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WEBP .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::AVIF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::SVG .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Icon .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WEBM .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WEBA .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::OGG .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::FLAC .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WAV .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::PDF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::TTF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::OTF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WOFF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WOFF2 .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::JsonApi .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::WASM .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::TIFF .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::AAC .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::Calendar .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::MPEG .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::TAR .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::GZIP .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::MOV .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::MP4 .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::ZIP .

Returns true if the top-level and sublevel types of self are the same as those of MediaType::EventStream .

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns a ContentType of Any, or */*.

The resulting type after dereferencing.

Dereferences the value.

Formats the ContentType as an HTTP Content-Type value.

Example

use rocket::http::ContentType;

let ct = format!("{}", ContentType::JSON);
assert_eq!(ct, "application/json");

Performs the conversion.

The associated error to be returned if derivation fails.

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

Parses a ContentType from a given Content-Type header value.

Examples

Parsing an application/json:

use std::str::FromStr;
use rocket::http::ContentType;

let json = ContentType::from_str("application/json").unwrap();
assert!(json.is_known());
assert_eq!(json, ContentType::JSON);

Parsing a content type extension:

use std::str::FromStr;
use rocket::http::ContentType;

let custom = ContentType::from_str("application/x-custom").unwrap();
assert!(!custom.is_known());
assert_eq!(custom.top(), "application");
assert_eq!(custom.sub(), "x-custom");

Parsing an invalid Content-Type value:

use std::str::FromStr;
use rocket::http::ContentType;

let custom = ContentType::from_str("application//x-custom");
assert!(custom.is_err());

The associated error which can be returned from parsing.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Creates a new Header with name Content-Type and the value set to the HTTP rendering of this Content-Type.

Performs the conversion.

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

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Converts self into a collection.

Should always be Self

The resulting type after obtaining ownership.

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.