Back to Alamofire

URLEncodedFormEncoder

docs/docsets/Alamofire.docset/Contents/Resources/Documents/Classes/URLEncodedFormEncoder.html

5.12.09.3 KB
Original Source

URLEncodedFormEncoder

public final class URLEncodedFormEncoder

An object that encodes instances into URL-encoded query strings.

ArrayEncoding can be used to configure how Array values are encoded. By default, the .brackets encoding is used, encoding array values with brackets for each value. e.g array[]=1&array[]=2.

BoolEncoding can be used to configure how Bool values are encoded. By default, the .numeric encoding is used, encoding true as 1 and false as 0.

DataEncoding can be used to configure how Data values are encoded. By default, the .deferredToData encoding is used, which encodes Data values using their default Encodable implementation.

DateEncoding can be used to configure how Date values are encoded. By default, the .deferredToDate encoding is used, which encodes Dates using their default Encodable implementation.

KeyEncoding can be used to configure how keys are encoded. By default, the .useDefaultKeys encoding is used, which encodes the keys directly from the Encodable implementation.

KeyPathEncoding can be used to configure how paths within nested objects are encoded. By default, the .brackets encoding is used, which encodes each sub-key in brackets. e.g. parent[child][grandchild]=value.

NilEncoding can be used to configure how nil Optional values are encoded. By default, the .dropKey encoding is used, which drops nil key / value pairs from the output entirely.

SpaceEncoding can be used to configure how spaces are encoded. By default, the .percentEscaped encoding is used, replacing spaces with %20.

This type is largely based on Vapor’s url-encoded-form project.

`

                ArrayEncoding
                `

Encoding to use for Array values.

See more

Declaration

Swift

public enum ArrayEncoding

`

                BoolEncoding
                `

Encoding to use for Bool values.

See more

Declaration

Swift

public enum BoolEncoding

`

                DataEncoding
                `

Encoding to use for Data values.

See more

Declaration

Swift

public enum DataEncoding

`

                DateEncoding
                `

Encoding to use for Date values.

See more

Declaration

Swift

public enum DateEncoding

`

                KeyEncoding
                `

Encoding to use for keys.

This type is derived from JSONEncoder‘s KeyEncodingStrategy and XMLEncoders KeyEncodingStrategy.

See more

Declaration

Swift

public enum KeyEncoding

`

                KeyPathEncoding
                `

Encoding to use for nested object and Encodable value key paths.

["parent" : ["child" : ["grandchild": "value"]]]

This encoding affects how the parent, child, grandchild path is encoded. Brackets are used by default. e.g. parent[child][grandchild]=value.

See more

Declaration

Swift

public struct KeyPathEncoding : Sendable

`

                NilEncoding
                `

Encoding to use for nil values.

See more

Declaration

Swift

public struct NilEncoding : Sendable

`

                SpaceEncoding
                `

Encoding to use for spaces.

See more

Declaration

Swift

public enum SpaceEncoding

`

                Error
                `

URLEncodedFormEncoder error.

See more

Declaration

Swift

public enum Error : Swift.Error

`

                alphabetizeKeyValuePairs
                `

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 to false, encoded Dictionary values may have a different encoded order each time they’re encoded due to Dictionary‘s random storage order, but Encodable types will maintain their encoded order.

Declaration

Swift

public let alphabetizeKeyValuePairs: Bool

`

                arrayEncoding
                `

The ArrayEncoding to use.

Declaration

Swift

public let arrayEncoding: ArrayEncoding

`

                boolEncoding
                `

The BoolEncoding to use.

Declaration

Swift

public let boolEncoding: BoolEncoding

`

                dataEncoding
                `

THe DataEncoding to use.

Declaration

Swift

public let dataEncoding: DataEncoding

`

                dateEncoding
                `

The DateEncoding to use.

Declaration

Swift

public let dateEncoding: DateEncoding

`

                keyEncoding
                `

The KeyEncoding to use.

Declaration

Swift

public let keyEncoding: KeyEncoding

`

                keyPathEncoding
                `

The KeyPathEncoding to use.

Declaration

Swift

public let keyPathEncoding: KeyPathEncoding

`

                nilEncoding
                `

The NilEncoding to use.

Declaration

Swift

public let nilEncoding: NilEncoding

`

                spaceEncoding
                `

The SpaceEncoding to use.

Declaration

Swift

public let spaceEncoding: SpaceEncoding

`

                allowedCharacters
                `

The CharacterSet of allowed (non-escaped) characters.

Declaration

Swift

public var allowedCharacters: CharacterSet

`

                init(alphabetizeKeyValuePairs:arrayEncoding:boolEncoding:dataEncoding:dateEncoding:keyEncoding:keyPathEncoding:nilEncoding: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,
            keyPathEncoding: KeyPathEncoding = .brackets,
            nilEncoding: NilEncoding = .dropKey,
            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.

| | nilEncoding |

The NilEncoding to use. .drop by default.

| | spaceEncoding |

The SpaceEncoding to use. .percentEscaped by default.

| | allowedCharacters |

The CharacterSet of allowed (non-escaped) characters. .afURLQueryAllowed by default.

|

`

                encode(_:)
                `

Encodes the value as a URL form encoded String.

Throws

An Error or EncodingError instance if encoding fails.

Declaration

Swift

public func encode(_ value: any Encodable) throws -> String

Parameters

| value |

The Encodable value.

|

Return Value

The encoded String.

`

                encode(_:)
                `

Encodes the value as Data. This is performed by first creating an encoded String and then returning the .utf8 data.

Throws

An Error or EncodingError instance if encoding fails.

Declaration

Swift

public func encode(_ value: any Encodable) throws -> Data

Parameters

| value |

The Encodable value.

|

Return Value

The encoded Data.