Vapor Documentation Beta

Structure URLEncoded​Form​Decoder

public struct URLEncodedFormDecoder: ContentDecoder, URLQueryDecoder

Decodes instances of Decodable types from application/x-www-form-urlencoded Data.

print(data) // "name=Vapor&age=3"
let user = try URLEncodedFormDecoder().decode(User.self, from: data)
print(user) // User

URL-encoded forms are commonly used by websites to send form data via POST requests. This encoding is relatively efficient for small amounts of data but must be percent-encoded. multipart/form-data is more efficient for sending large data blobs like files.

See Mozilla's docs for more information about url-encoded forms.

%3 URLEncodedFormDecoder URLEncodedFormDecoder ContentDecoder ContentDecoder URLEncodedFormDecoder->ContentDecoder URLQueryDecoder URLQueryDecoder URLEncodedFormDecoder->URLQueryDecoder

Nested Types

URLEncodedFormDecoder.Configuration

Used to capture URLForm Coding Configuration used for decoding

Conforms To

ContentDecoder
URLQueryDecoder

Initializers

init(configuration:​)

public init(configuration: Configuration = .init())

Create a new URLEncodedFormDecoder. Can be configured by using the global ContentConfiguration class

ContentConfiguration.global.use(urlDecoder: URLEncodedFormDecoder(bracketsAsArray: true, flagsAsBool: true, arraySeparator: nil))

Parameters

configuration Configuration

Defines how decoding is done see URLEncodedFormCodingConfig for more information

Methods

decode(_:​from:​headers:​)

public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders) throws -> D where D: Decodable

ContentDecoder conformance.

decode(_:​from:​)

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

Decodes the URL's query string to the type provided

let ziz = try URLEncodedFormDecoder().decode(Pet.self, from: "name=Ziz&type=cat")

Parameters

decodable D.​Type

Type to decode to

url URI

URL to read the query string from

configuration

Overwrides the default coding configuration

decode(_:​from:​)

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

Decodes an instance of the supplied Decodable type from Data.

print(data) // "name=Vapor&age=3"
let user = try URLEncodedFormDecoder().decode(User.self, from: data)
print(user) // User

Parameters

decodable D.​Type

Generic Decodable type (D) to decode.

from String

Data to decode a D from.

configuration

Overwrides the default coding configuration

Throws

Any error that may occur while attempting to decode the specified type.

Returns

An instance of the Decodable type (D).