Vapor Documentation

Protocol Content​Container

public protocol ContentContainer  

Default Implementations

decode(_:​)

public func decode<D>(_ content: D.Type) throws -> D where D: Decodable  

decode(_:​)

public func decode<C>(_ decodable: C.Type) throws -> C where C: Content  

encode(_:​)

public mutating func encode<C>(_ encodable: C) throws
        where C: Content

Serializes an Encodable object to this message using specific HTTPMessageEncoder.

try req.content.encode(user, using: JSONEncoder())

Parameters

encodable C

Instance of generic Encodable to serialize to this HTTP message.

Throws

Errors during serialization.

encode(_:​as:​)

public mutating func encode<E>(_ encodable: E, as contentType: HTTPMediaType) throws
        where E: Encodable

Serializes an Encodable object to this message using specific HTTPMessageEncoder.

try req.content.encode(user, using: JSONEncoder())

Parameters

encodable E

Instance of generic Encodable to serialize to this HTTP message.

encoder

Specific HTTPMessageEncoder to use.

Throws

Errors during serialization.

encode(_:​as:​)

public mutating func encode<C>(_ content: C, as contentType: HTTPMediaType) throws
        where C: Content

Serializes a Content object to this message using specific HTTPMessageEncoder.

try req.content.encode(user, using: JSONEncoder())

Parameters

content C

Instance of generic Content to serialize to this HTTP message.

encoder

Specific HTTPMessageEncoder to use.

Throws

Errors during serialization.

subscript(_:​)

public subscript<D>(_ keyPath: CodingKeyRepresentable...) -> D?
        where D: Decodable

Fetches a single Decodable value at the supplied key-path from this HTTP request's query string.

Note: This is a non-throwing subscript convenience method for get(_:at:).

let name: String? = req.query["user", "name"]
print(name) /// String?

Parameters

key​Path Coding​Key​Representable

One or more key path components to the desired value.

Returns

Decoded Decodable value.

subscript(_:​at:​)

public subscript<D>(_ type: D.Type, at keyPath: CodingKeyRepresentable...) -> D?
        where D: Decodable

Fetches a single Decodable value at the supplied key-path from this HTTP request's query string.

Note: This is a non-throwing subscript convenience method for get(_:at:).

let name = req.query[String.self, at: "user", "name"]
print(name) /// String?

Parameters

type D.​Type

The Decodable value type to decode.

key​Path Coding​Key​Representable

One or more key path components to the desired value.

Returns

Decoded Decodable value.

subscript(_:​at:​)

public subscript<D>(_ type: D.Type, at keyPath: [CodingKeyRepresentable]) -> D?
        where D: Decodable

Fetches a single Decodable value at the supplied key-path from this HTTP request's query string.

Note: This is a non-throwing subscript convenience method for get(_:at:). This is the non-variadic version.

let name = req.query[String.self, at: "user", "name"]
print(name) /// String?

Parameters

type D.​Type

The Decodable value type to decode.

key​Path [Coding​Key​Representable]

One or more key path components to the desired value.

Returns

Decoded Decodable value.

get(_:​at:​)

public func get<D>(_ type: D.Type = D.self, at keyPath: CodingKeyRepresentable...) throws -> D
        where D: Decodable

Fetches a single Decodable value at the supplied key-path from this HTTP request's body.

let name = try req.content.get(String.self, at: "user", "name")
print(name) /// String

Parameters

type D.​Type

The Decodable value type to decode.

key​Path Coding​Key​Representable

One or more key path components to the desired value.

Returns

Decoded Decodable value.

get(_:​at:​)

public func get<D>(_ type: D.Type = D.self, at keyPath: [CodingKeyRepresentable]) throws -> D
        where D: Decodable

Fetches a single Decodable value at the supplied key-path from this HTTP request's body.

Note: This is the non-variadic version.

let name = try req.content.get(String.self, at: "user", "name")
print(name) /// String

Parameters

type D.​Type

The Decodable value type to decode.

key​Path [Coding​Key​Representable]

One or more key path components to the desired value.

Returns

Decoded Decodable value.

Requirements

content​Type

var contentType: HTTPMediaType?  

decode(_:​using:​)

func decode<D>(_ decodable: D.Type, using decoder: ContentDecoder) throws -> D
        where D: Decodable

encode(_:​using:​)

mutating func encode<E>(_ encodable: E, using encoder: ContentEncoder) throws
        where E: Encodable