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

  • 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
  • Configures how Array parameters are encoded.

    See more

    Declaration

    Swift

    public enum ArrayEncoding : Sendable
  • Configures how Bool parameters are encoded.

    See more

    Declaration

    Swift

    public enum BoolEncoding : Sendable

Properties

  • Returns a default URLEncoding instance with a .methodDependent destination.

    Declaration

    Swift

    public static var `default`: URLEncoding { get }
  • Returns a URLEncoding instance with a .queryString destination.

    Declaration

    Swift

    public static var queryString: URLEncoding { get }
  • Returns a URLEncoding instance with an .httpBody destination.

    Declaration

    Swift

    public static var httpBody: URLEncoding { get }
  • The destination defining where the encoded query string is to be applied to the URL request.

    Declaration

    Swift

    public let destination: Destination
  • The encoding to use for Array parameters.

    Declaration

    Swift

    public let arrayEncoding: ArrayEncoding
  • The encoding to use for Bool parameters.

    Declaration

    Swift

    public let boolEncoding: BoolEncoding

Initialization

Encoding

  • Declaration

    Swift

    public func encode(_ urlRequest: any URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
  • 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.

  • 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.