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:

  1. The type and subtype of a media type are case-insensitive.
  2. The key of a parameter is case-insensitive but the value is case-sensitive.
  3. The parameters of a media type do not effect its value.

The implications of these for MediaType are:

  1. The implementations of Equatable and Hashable are case-insensitive.
  2. Ditto for the parameters of a MediaType.
  3. The parameters of a MediaType are not used in the Equatable and Hashable implementation of MediaType.

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., application, text, audio etc.

    See more

    Declaration

    Swift

    public struct TopLevelType : Hashable, RawRepresentable, CustomStringConvertible
  • A sub type of a top level media type, e.g., plain, css, json etc.

    See more

    Declaration

    Swift

    public struct SubType : Hashable, RawRepresentable, CustomStringConvertible
  • 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
  • xml

    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
  • css

    Undocumented

    Declaration

    Swift

    static let css: MediaType
  • gif

    Undocumented

    Declaration

    Swift

    static let gif: MediaType
  • png

    Undocumented

    Declaration

    Swift

    static let png: MediaType
  • Undocumented

    Declaration

    Swift

    static let jpeg: MediaType
  • svg

    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