Back to Alamofire

URLEncoding

docs/Structs/URLEncoding.html

5.12.05.0 KB
Original Source

URLEncoding

public struct URLEncoding : ParameterEncoding

Creates a url-encoded query string to be set as or appended to any existing URL query string or set as the HTTP body of the URL request. Whether the query string is set or appended to any existing URL query string or set as the HTTP body depends on the destination of the encoding.

The Content-Type HTTP header field of an encoded request with HTTP body is set to application/x-www-form-urlencoded; charset=utf-8.

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 boolean values are encoded. The default behavior is to encode true as 1 and false as 0.

Helper Types

`

                Destination
                `

Defines whether the url-encoded query string is applied to the existing query string or HTTP body of the resulting URL request.

See more

Declaration

Swift

public enum Destination : Sendable

`

                ArrayEncoding
                `

Configures how Array parameters are encoded.

See more

Declaration

Swift

public enum ArrayEncoding : Sendable

`

                BoolEncoding
                `

Configures how Bool parameters are encoded.

See more

Declaration

Swift

public enum BoolEncoding : Sendable

Properties

`

                default
                `

Returns a default URLEncoding instance with a .methodDependent destination.

Declaration

Swift

public static var `default`: URLEncoding { get }

`

                queryString
                `

Returns a URLEncoding instance with a .queryString destination.

Declaration

Swift

public static var queryString: URLEncoding { get }

`

                httpBody
                `

Returns a URLEncoding instance with an .httpBody destination.

Declaration

Swift

public static var httpBody: URLEncoding { get }

`

                destination
                `

The destination defining where the encoded query string is to be applied to the URL request.

Declaration

Swift

public let destination: Destination

`

                arrayEncoding
                `

The encoding to use for Array parameters.

Declaration

Swift

public let arrayEncoding: ArrayEncoding

`

                boolEncoding
                `

The encoding to use for Bool parameters.

Declaration

Swift

public let boolEncoding: BoolEncoding

Initialization

`

                init(destination:arrayEncoding:boolEncoding:)
                `

Creates an instance using the specified parameters.

Declaration

Swift

public init(destination: Destination = .methodDependent,
            arrayEncoding: ArrayEncoding = .brackets,
            boolEncoding: BoolEncoding = .numeric)

Parameters

| destination |

Destination defining where the encoded query string will be applied. .methodDependent by default.

| | arrayEncoding |

ArrayEncoding to use. .brackets by default.

| | boolEncoding |

BoolEncoding to use. .numeric by default.

|

Encoding

`

                encode(_:with:)
                `

Declaration

Swift

public func encode(_ urlRequest: any URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest

`

                queryComponents(fromKey:value:)
                `

Creates a percent-escaped, URL encoded query string components from the given key-value pair recursively.

Declaration

Swift

public func queryComponents(fromKey key: String, value: Any) -> [(String, String)]

Parameters

| key |

Key of the query component.

| | value |

Value of the query component.

|

Return Value

The percent-escaped, URL encoded query string components.

`

                escape(_:)
                `

Creates a percent-escaped string following RFC 3986 for a query string key or value.

Declaration

Swift

public func escape(_ string: String) -> String

Parameters

| string |

String to be percent-escaped.

|

Return Value

The percent-escaped String.