MediaType
public struct MediaType : CustomStringConvertible
A media type (MIME type) used to identify content.
Media types consist of a top level type
a subtype
and zero or more parameters (key-value pairs). The
implementation in Requests is based on RFC 2045 and RFC
6838. The key take-aways from these RFCs are:
- The
type
andsubtype
of a media type are case-insensitive. - The key of a parameter is case-insensitive but the value is case-sensitive.
- The parameters of a media type do not effect its value.
The implications of these for MediaType
are:
- The implementations of
Equatable
andHashable
are case-insensitive. - Ditto for the parameters of a
MediaType
. - The
parameters
of aMediaType
are not used in theEquatable
andHashable
implementation ofMediaType
.
Defining New Media Types
Several common media types are defined as static properties on MediaType
. You can add new types in an extension of
MediaType
. Define the new subtype in an extension of MediaType.SubType
and the new media type in an extension of
MediaType
For media types with required parameters (e.g., multipart/form-data
), define the media type as a static function
of its required parameters. For example, MediaType.formData
is defined as:
static let formData: (String) -> MediaType = { boundary in
MediaType(type: .multipart, subtype: .formData, parameters: ["boundary": boundary])
}
which will produce a new MediaType
with the boundary
parameter set to the provided String
.
-
A top level media type, e.g.,
See moreapplication
,text
,audio
etc.Declaration
Swift
public struct TopLevelType : Hashable, RawRepresentable, CustomStringConvertible
-
Undocumented
Declaration
Swift
public let type: TopLevelType
-
Undocumented
Declaration
Swift
public let subtype: SubType
-
Undocumented
Declaration
Swift
public var parameters: [CaseInsensitiveString : String]
-
Undocumented
Declaration
Swift
public var rawValue: String { get }
-
Declaration
Swift
public var description: String { get }
-
Undocumented
Declaration
Swift
public init(type: TopLevelType, subtype: SubType, parameters: [CaseInsensitiveString : String] = [:])
-
Declaration
Swift
public static func == (lhs: MediaType, rhs: MediaType) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
-
Undocumented
Declaration
Swift
static let json: MediaType
-
Undocumented
Declaration
Swift
static let xml: MediaType
-
Undocumented
Declaration
Swift
static let urlEncodedForm: MediaType
-
Undocumented
Declaration
Swift
static let binary: MediaType
-
Undocumented
Declaration
Swift
static let plainText: MediaType
-
Undocumented
Declaration
Swift
static let html: MediaType
-
Undocumented
Declaration
Swift
static let css: MediaType
-
Undocumented
Declaration
Swift
static let gif: MediaType
-
Undocumented
Declaration
Swift
static let png: MediaType
-
Undocumented
Declaration
Swift
static let jpeg: MediaType
-
Undocumented
Declaration
Swift
static let svg: MediaType
-
Constructs a
multipart/form-data
media type using the given boundary.Declaration
Swift
static let formData: (String) -> MediaType