ContentConfig

public struct ContentConfig : Service, ServiceType

Configures which Encoders and Decoders to use when interacting with data in HTTP messages.

var contentConfig = ContentConfig()
contentConfig.use(encoder: JSONEncoder(), for: .json)
services.register(contentConfig)

Each coder is registered to a specific MediaType. When decoding content from HTTP messages, the MediaType will be specified by the message itself. When encoding content from HTTP messages, the MediaType should be specified (MediaType.json is usually the assumed default).

try res.content.encode("hello", as: .plainText)
print(res.mediaType) // .plainText
print(res.http.body) // "hello"

Most often, these configured coders are used to encode and decode types conforming to Content. See the Content protocol for more information.

  • Creates a ContentConfig containing all of Vapor’s default coders.

    var contentConfig = ContentConfig.default()
    // add or replace coders
    services.register(contentConfig)
    

    Declaration

    Swift

    public static func `default`() -> ContentConfig
  • Adds an HTTPMessage and DataEncoder for the specified MediaType.

    contentConfig.use(encoder: JSONEncoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(encoder: HTTPMessageEncoder & DataEncoder, for mediaType: MediaType)

    Parameters

    encoder

    Encoder to use.

    mediaType

    Encoder will be used to encode this MediaType.

  • Adds an HTTPMessage and DataDecoder for the specified MediaType.

    contentConfig.use(decoder: JSONDecoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(decoder: HTTPMessageDecoder & DataDecoder, for mediaType: MediaType)

    Parameters

    decoder

    Decoder to use.

    mediaType

    Decoder will be used to decode this MediaType.

  • Adds an HTTPMessage and DataEncoder by type for the specified MediaType.

    Note

    The type will be resolved from the service-container at boot.

    contentConfig.use(encoder: JSONEncoder.self, for: .json)

    Declaration

    Swift

    public mutating func use<B>(encoder: B.Type, for mediaType: MediaType) where B : DataEncoder, B : HTTPMessageEncoder

    Parameters

    encoder

    Encoder type to use.

    mediaType

    Encoder will be used to encode this MediaType.

  • Adds an HTTPMessage and DataDecoder by type for the specified MediaType.

    Note

    The type will be resolved from the service-container at boot.

    contentConfig.use(decoder: JSONDecoder.self, for: .json)

    Declaration

    Swift

    public mutating func use<B>(decoder: B.Type, for mediaType: MediaType) where B : DataDecoder, B : HTTPMessageDecoder

    Parameters

    decoder

    Decoder type to use.

    mediaType

    Decoder will be used to decode this MediaType.

  • Adds an HTTPMessageEncoder for the specified MediaType.

    contentConfig.use(httpEncoder: JSONEncoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(httpEncoder: HTTPMessageEncoder, for mediaType: MediaType)

    Parameters

    httpEncoder

    HTTPMessageEncoder to use.

    mediaType

    HTTPMessageEncoder will be used to encode this MediaType.

  • Adds an HTTPMessageDecoder for the specified MediaType.

    contentConfig.use(httpDecoder: JSONDecoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(httpDecoder: HTTPMessageDecoder, for mediaType: MediaType)

    Parameters

    httpDecoder

    HTTPMessageDecoder to use.

    mediaType

    HTTPMessageDecoder will be used to decode this MediaType.

  • Adds an HTTPMessageEncoder by type for the specified MediaType.

    contentConfig.use(httpEncoder: JSONEncoder.self, for: .json)
    

    Declaration

    Swift

    public mutating func use<B>(httpEncoder: B.Type, for mediaType: MediaType) where B : HTTPMessageEncoder

    Parameters

    httpEncoder

    HTTPMessageEncoder type to use.

    mediaType

    HTTPMessageEncoder will be used to encode this MediaType.

  • Adds an HTTPMessageDecoder by type for the specified MediaType.

    contentConfig.use(httpDecoder: JSONDecoder.self, for: .json)
    

    Declaration

    Swift

    public mutating func use<B>(httpDecoder: B.Type, for mediaType: MediaType) where B : HTTPMessageDecoder

    Parameters

    httpDecoder

    HTTPMessageDecoder type to use.

    mediaType

    HTTPMessageDecoder will be used to decode this MediaType.

  • Adds an DataEncoder for the specified MediaType.

    contentConfig.use(dataEncoder: JSONEncoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(dataEncoder: DataEncoder, for mediaType: MediaType)

    Parameters

    dataEncoder

    DataEncoder to use.

    mediaType

    DataEncoder will be used to encode this MediaType.

  • Adds an DataDecoder for the specified MediaType.

    contentConfig.use(dataDecoder: JSONDecoder(), for: .json)
    

    Declaration

    Swift

    public mutating func use(dataDecoder: DataDecoder, for mediaType: MediaType)

    Parameters

    dataDecoder

    DataDecoder to use.

    mediaType

    DataDecoder will be used to decode this MediaType.

  • Adds an DataEncoder by type for the specified MediaType.

    contentConfig.use(dataEncoder: JSONEncoder.self, for: .json)
    

    Declaration

    Swift

    public mutating func use<D>(dataEncoder: D.Type, for mediaType: MediaType) where D : DataEncoder

    Parameters

    dataEncoder

    DataEncoder type to use.

    mediaType

    DataEncoder will be used to encode this MediaType.

  • Adds an DataDecoder by type for the specified MediaType.

    contentConfig.use(dataDecoder: JSONDecoder.self, for: .json)
    

    Declaration

    Swift

    public mutating func use<D>(dataDecoder: D.Type, for mediaType: MediaType) where D : DataDecoder

    Parameters

    dataDecoder

    DataDecoder type to use.

    mediaType

    DataDecoder will be used to decode this MediaType.