Vapor Documentation

Structure HTTPMedia​Type

public struct HTTPMediaType: Hashable, CustomStringConvertible, Equatable  

Represents an encoded data-format, used in HTTP, HTML, email, and elsewhere.

text/plain
application/json; charset=utf8

Description from rfc2045:

In general, the top-level media type is used to declare the general
type of data, while the subtype specifies a specific format for that
type of data.  Thus, a media type of "image/xyz" is enough to tell a
user agent that the data is an image, even if the user agent has no
knowledge of the specific image format "xyz".  Such information can
be used, for example, to decide whether or not to show a user the raw
data from an unrecognized subtype -- such an action might be
reasonable for unrecognized subtypes of text, but not for
unrecognized subtypes of image or audio.  For this reason, registered
subtypes of text, image, audio, and video should not contain embedded
information that is really of a different type.  Such compound
formats should be represented using the "multipart" or "application"
types.

Simplified format:

mediaType := type "/" subtype *(";" parameter)
; Matching of media type and subtype
; is ALWAYS case-insensitive.

type := token

subtype := token

parameter := attribute "=" value

attribute := token
; Matching of attributes
; is ALWAYS case-insensitive.

token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
    or tspecials>

value := token
; token MAY be quoted

tspecials :=  "(" / ")" / "<" / ">" / "@" /
              "," / ";" / ":" / "\" / <">
              "/" / "[" / "]" / "?" / "="
; Must be in quoted-string,
; to use within parameter values
%609 HTTPMediaType HTTPMediaType CustomStringConvertible CustomStringConvertible HTTPMediaType->CustomStringConvertible Equatable Equatable HTTPMediaType->Equatable Hashable Hashable HTTPMediaType->Hashable

Conforms To

CustomStringConvertible
Equatable
Hashable

Initializers

init(type:​sub​Type:​parameters:​)

public init(type: String, subType: String, parameters: [String: String] = [:])  

Create a new MediaType.

Properties

type

public var type: String

The MediaType's discrete or composite type. Usually one of the following.

"text" / "image" / "audio" / "video" / "application
"message" / "multipart"
...

In the MediaType "application/json; charset=utf8":

  • type: "application"

  • subtype: "json"

sub​Type

public var subType: String

The MediaType's specific type. Usually a unique string.

In the MediaType "application/json; charset=utf8":

  • type: "application"

  • subtype: "json"

parameters

public var parameters: [String: String] 

The MediaType's metadata. Zero or more key/value pairs.

In the MediaType "application/json; charset=utf8":

  • type: "application"

  • subtype: "json"

description

public var description: String  

See CustomStringConvertible.

any

static let any  

Any media type (/).

plain​Text

static let plainText  

Plain text media type.

html

static let html  

HTML media type.

css

static let css  

CSS media type.

url​Encoded​Form

static let urlEncodedForm  

URL encoded form media type.

form​Data

static let formData  

Multipart encoded form data.

multipart

static let multipart  

Mixed multipart encoded data.

json

static let json  

JSON media type.

json​API

static let jsonAPI  

JSON API media type.

json​Sequence

static let jsonSequence  

JSON sequence media type.

xml

static let xml  

XML media type.

dtd

static let dtd  

DTD media type.

pdf

static let pdf  

PDF data.

zip

static let zip  

Zip file.

tar

static let tar  

tar file.

gzip

static let gzip  

Gzip file.

bzip2

static let bzip2  

Bzip2 file.

binary

static let binary  

Binary data.

gif

static let gif  

GIF image.

jpeg

static let jpeg  

JPEG image.

png

static let png  

PNG image.

svg

static let svg  

SVG image.

audio

static let audio  

Basic audio.

midi

static let midi  

MIDI audio.

mp3

static let mp3  

MP3 audio.

wave

static let wave  

Wave audio.

ogg

static let ogg  

OGG audio.

avi

static let avi  

AVI video.

mpeg

static let mpeg  

MPEG video.

Methods

serialize()

public func serialize() -> String  

Converts this MediaType into its string representation.

For example, the following media type:

  • type: "application"

  • subtype: "json"

Would be converted to "application/json; charset=utf8".

hash(into:​)

public func hash(into hasher: inout Hasher)  

See Hashable.

file​Extension(_:​)

public static func fileExtension(_ ext: String) -> HTTPMediaType?  

Creates a MediaType from a file extension, if possible.

guard let mediaType = MediaType.fileExtension("txt") else { ... }

Parameters

ext String

File extension (ie., "txt", "json", "html").

Returns

Newly created MediaType, nil if none was found.

form​Data(boundary:​)

static func formData(boundary: String) -> HTTPMediaType  

Operators

==

public static func ==(lhs: HTTPMediaType, rhs: HTTPMediaType) -> Bool  

See Equatable.