Vapor Documentation Beta

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
%3 HTTPMediaType HTTPMediaType Hashable Hashable HTTPMediaType->Hashable CustomStringConvertible CustomStringConvertible HTTPMediaType->CustomStringConvertible Equatable Equatable HTTPMediaType->Equatable

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

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

var subType: String

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

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

  • type: "application"

  • subtype: "json"

parameters

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

var description: String

See CustomStringConvertible.

any

let any

Any media type (/).

plain​Text

let plainText

Plain text media type.

html

let html

HTML media type.

css

let css

CSS media type.

url​Encoded​Form

let urlEncodedForm

URL encoded form media type.

form​Data

let formData

Multipart encoded form data.

multipart

let multipart

Mixed multipart encoded data.

json

let json

JSON media type.

json​API

let jsonAPI

JSON API media type (see https://jsonapi.org/format/).

xml

let xml

XML media type.

dtd

let dtd

DTD media type.

pdf

let pdf

PDF data.

zip

let zip

Zip file.

tar

let tar

tar file.

gzip

let gzip

Gzip file.

bzip2

let bzip2

Bzip2 file.

binary

let binary

Binary data.

gif

let gif

GIF image.

jpeg

let jpeg

JPEG image.

png

let png

PNG image.

svg

let svg

SVG image.

audio

let audio

Basic audio.

midi

let midi

MIDI audio.

mp3

let mp3

MP3 audio.

wave

let wave

Wave audio.

ogg

let ogg

OGG audio.

avi

let avi

AVI video.

mpeg

let mpeg

MPEG video.

Methods

==(lhs:​rhs:​)

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

See Equatable.

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