Vapor Documentation

Protocol URLQuery​Container

public protocol URLQueryContainer  

Helper for encoding and decoding data from an HTTP request query string.

See Request.query for more information.

Default Implementations

encode(_:​)

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

Serializes a Content type to this HTTP request query string.

let flags: Flags ...
try req.query.encode(flags)

A MediaType.urlEncodedForm encoder will be used.

Parameters

content C

Content type to encode to this HTTP message.

Throws

Any errors making the decoder for this media type or serializing the query string.

decode(_:​)

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

Parses a Content type from this HTTP request query string.

let flags = try req.query.decode(Flags.self)
print(flags) // Flags

A MediaType.urlEncodedForm decoder will be used.

Parameters

content C.​Type

Content type to decode from this HTTP message.

Throws

Any errors making the decoder for this media type or parsing the query string.

Returns

Instance of the Decodable type.

encode(_:​)

public mutating func encode<E>(_ encodable: E) throws where E: Encodable  

Serializes an Encodable type to this HTTP request query string.

let flags: Flags ...
try req.query.encode(flags)

A MediaType.urlEncodedForm encoder will be used.

Parameters

encodable E

Encodable type to encode to this HTTP message.

Throws

Any errors making the decoder for this media type or serializing the query string.

decode(_:​)

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

Parses a Decodable type from this HTTP request query string.

let flags = try req.query.decode(Flags.self)
print(flags) // Flags

A MediaType.urlEncodedForm decoder will be used.

Parameters

decodable D.​Type

Decodable type to decode from this HTTP message.

Throws

Any errors making the decoder for this media type or parsing the query string.

Returns

Instance of the Decodable type.

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 query string.

let name = try req.query.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 query string.

Note: This is the non-variadic version.

let name = try req.query.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

decode(_:​using:​)

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

encode(_:​using:​)

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