URLEncodedFormDecoder
public struct URLEncodedFormDecoder
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.
-
Used to capture URLForm Coding Configuration used for decoding
See moreDeclaration
Swift
public struct Configuration
-
Create a new
URLEncodedFormDecoder
. Can be configured by using the globalContentConfiguration
classContentConfiguration.global.use(urlDecoder: URLEncodedFormDecoder(bracketsAsArray: true, flagsAsBool: true, arraySeparator: nil))
Declaration
Swift
public init( configuration: Configuration = .init() )
Parameters
configuration
Defines how decoding is done see
URLEncodedFormCodingConfig
for more information -
Decodes the URL’s query string to the type provided
let ziz = try URLEncodedFormDecoder().decode(Pet.self, from: "name=Ziz&type=cat")
Declaration
Swift
public func decode<D>(_ decodable: D.Type, from url: URL) throws -> D where D : Decodable
Parameters
decodable
Type to decode to
url
URL to read the query string from
configuration
Overwrides the default coding configuration
-
Decodes an instance of the supplied
Decodable
type fromData
.print(data) // "name=Vapor&age=3" let user = try URLEncodedFormDecoder().decode(User.self, from: data) print(user) // User
Throws
Any error that may occur while attempting to decode the specified type.Declaration
Swift
public func decode<D>(_ decodable: D.Type, from string: String) throws -> D where D : Decodable
Parameters
decodable
Generic
Decodable
type (D
) to decode.from
Data
to decode aD
from.configuration
Overwrides the default coding configuration
Return Value
An instance of the
Decodable
type (D
).