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.
-
Defines whether the url-encoded query string is applied to the existing query string or HTTP body of the resulting URL request.
See moreDeclaration
Swift
public enum Destination : Sendable -
Configures how
See moreArrayparameters are encoded.Declaration
Swift
public enum ArrayEncoding : Sendable -
Configures how
See moreBoolparameters are encoded.Declaration
Swift
public enum BoolEncoding : Sendable
-
Returns a default
URLEncodinginstance with a.methodDependentdestination.Declaration
Swift
public static var `default`: URLEncoding { get } -
Returns a
URLEncodinginstance with a.queryStringdestination.Declaration
Swift
public static var queryString: URLEncoding { get } -
Returns a
URLEncodinginstance with an.httpBodydestination.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
Arrayparameters.Declaration
Swift
public let arrayEncoding: ArrayEncoding -
The encoding to use for
Boolparameters.Declaration
Swift
public let boolEncoding: BoolEncoding
-
Creates an instance using the specified parameters.
Declaration
Swift
public init(destination: Destination = .methodDependent, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric)Parameters
destinationDestinationdefining where the encoded query string will be applied..methodDependentby default.arrayEncodingArrayEncodingto use..bracketsby default.boolEncodingBoolEncodingto use..numericby default.
-
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
keyKey of the query component.
valueValue 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) -> StringParameters
stringStringto be percent-escaped.Return Value
The percent-escaped
String.
View on GitHub
Install in Dash