URLEncodedFormEncoder
public final class URLEncodedFormEncoder
An object that encodes instances into URL-encoded query strings.
There is no published specification for how to encode collection types. By default, the convention of appending
[]
to the key for array values (foo[]=1&foo[]=2
), and appending the key surrounded by square brackets for
nested dictionary values (foo[bar]=baz
) is used. Optionally, ArrayEncoding
can be used to omit the
square brackets appended to array keys.
BoolEncoding
can be used to configure how Bool
values are encoded. The default behavior is to encode
true
as 1 and false
as 0.
DateEncoding
can be used to configure how Date
values are encoded. By default, the .deferredToDate
strategy is used, which formats dates from their structure.
SpaceEncoding
can be used to configure how spaces are encoded. Modern encodings use percent replacement (%20
),
while older encodings may expect spaces to be replaced with +
.
This type is largely based on Vapor’s url-encoded-form
project.
-
Encoding to use for
See moreArray
values.Declaration
Swift
public enum ArrayEncoding
-
Encoding to use for
See moreBool
values.Declaration
Swift
public enum BoolEncoding
-
Encoding to use for
See moreData
values.Declaration
Swift
public enum DataEncoding
-
Encoding to use for
See moreDate
values.Declaration
Swift
public enum DateEncoding
-
Encoding to use for keys.
This type is derived from
See moreJSONEncoder
‘sKeyEncodingStrategy
andXMLEncoder
sKeyEncodingStrategy
.Declaration
Swift
public enum KeyEncoding
-
Encoding to use for spaces.
See moreDeclaration
Swift
public enum SpaceEncoding
-
See moreURLEncodedFormEncoder
error.Declaration
Swift
public enum Error : Swift.Error
-
Whether or not to sort the encoded key value pairs.
Note
This setting ensures a consistent ordering for all encodings of the same parameters. When set tofalse
, encodedDictionary
values may have a different encoded order each time they’re encoded due toDictionary
‘s random storage order, butEncodable
types will maintain their encoded order.Declaration
Swift
public let alphabetizeKeyValuePairs: Bool
-
The
ArrayEncoding
to use.Declaration
Swift
public let arrayEncoding: ArrayEncoding
-
The
BoolEncoding
to use.Declaration
Swift
public let boolEncoding: BoolEncoding
-
THe
DataEncoding
to use.Declaration
Swift
public let dataEncoding: DataEncoding
-
The
DateEncoding
to use.Declaration
Swift
public let dateEncoding: DateEncoding
-
The
KeyEncoding
to use.Declaration
Swift
public let keyEncoding: KeyEncoding
-
The
SpaceEncoding
to use.Declaration
Swift
public let spaceEncoding: SpaceEncoding
-
The
CharacterSet
of allowed (non-escaped) characters.Declaration
Swift
public var allowedCharacters: CharacterSet
-
init(alphabetizeKeyValuePairs:
arrayEncoding: boolEncoding: dataEncoding: dateEncoding: keyEncoding: spaceEncoding: allowedCharacters: ) Creates an instance from the supplied parameters.
Declaration
Swift
public init(alphabetizeKeyValuePairs: Bool = true, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric, dataEncoding: DataEncoding = .base64, dateEncoding: DateEncoding = .deferredToDate, keyEncoding: KeyEncoding = .useDefaultKeys, spaceEncoding: SpaceEncoding = .percentEscaped, allowedCharacters: CharacterSet = .afURLQueryAllowed)
Parameters
alphabetizeKeyValuePairs
Whether or not to sort the encoded key value pairs.
true
by default.arrayEncoding
The
ArrayEncoding
to use..brackets
by default.boolEncoding
The
BoolEncoding
to use..numeric
by default.dataEncoding
The
DataEncoding
to use..base64
by default.dateEncoding
The
DateEncoding
to use..deferredToDate
by default.keyEncoding
The
KeyEncoding
to use..useDefaultKeys
by default.spaceEncoding
The
SpaceEncoding
to use..percentEscaped
by default.allowedCharacters
The
CharacterSet
of allowed (non-escaped) characters..afURLQueryAllowed
by default. -
Encodes the
value
as a URL form encodedString
.Throws
An
Error
orEncodingError
instance if encoding fails.Declaration
Swift
public func encode(_ value: Encodable) throws -> String
Parameters
value
The
Encodable
value.`Return Value
The encoded
String
. -
Encodes the value as
Data
. This is performed by first creating an encodedString
and then returning the.utf8
data.Throws
An
Error
orEncodingError
instance if encoding fails.Declaration
Swift
public func encode(_ value: Encodable) throws -> Data
Parameters
value
The
Encodable
value.Return Value
The encoded
Data
.