Vapor Documentation Beta

Structure Content​Configuration

public struct ContentConfiguration

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

ContentConfiguration.global.use(encoder: JSONEncoder(), for: .json)

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.

Initializers

init()

public init()

Create a new, empty ContentConfig.

Properties

global

var global: ContentConfiguration

Methods

`default`()

public static func `default`() -> ContentConfiguration

Creates a ContentConfiguration containing all of Vapor's default coders.

use(encoder:​for:​)

public mutating func use(encoder: ContentEncoder, for mediaType: HTTPMediaType)

Adds an ContentEncoder for the specified HTTPMediaType.

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

Parameters

encoder Content​Encoder

ContentEncoder to use.

media​Type HTTPMedia​Type

ContentEncoder will be used to encode this HTTPMediaType.

use(decoder:​for:​)

public mutating func use(decoder: ContentDecoder, for mediaType: HTTPMediaType)

Adds a ContentDecoder for the specified HTTPMediaType.

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

Parameters

decoder Content​Decoder

ContentDecoder to use.

media​Type HTTPMedia​Type

ContentDecoder will be used to decode this HTTPMediaType.

use(url​Encoder:​)

public mutating func use(urlEncoder: URLQueryEncoder)

use(url​Decoder:​)

public mutating func use(urlDecoder: URLQueryDecoder)

require​Encoder(for:​)

public func requireEncoder(for mediaType: HTTPMediaType) throws -> ContentEncoder

Returns an HTTPMessageEncoder for the specified MediaType or throws an error.

let coder = try coders.requireHTTPEncoder(for: .json)

Parameters

HTTPMedia​Type

An encoder for this MediaType will be returned.

require​Decoder(for:​)

public func requireDecoder(for mediaType: HTTPMediaType) throws -> ContentDecoder

Returns a HTTPMessageDecoder for the specified MediaType or throws an error.

let coder = try coders.requireHTTPDecoder(for: .json)

Parameters

HTTPMedia​Type

A decoder for this MediaType will be returned.

require​URLEncoder()

public func requireURLEncoder() throws -> URLQueryEncoder

Returns an HTTPMessageEncoder for the specified MediaType or throws an error.

let coder = try coders.requireHTTPEncoder(for: .json)

Parameters

HTTPMedia​Type

An encoder for this MediaType will be returned.

require​URLDecoder()

public func requireURLDecoder() throws -> URLQueryDecoder

Returns a HTTPMessageDecoder for the specified MediaType or throws an error.

let coder = try coders.requireHTTPDecoder(for: .json)

Parameters

HTTPMedia​Type

A decoder for this MediaType will be returned.