URLEncoding - SwiftDocOrg/Alamofire GitHub Wiki
URLEncoding
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.
public struct URLEncoding: ParameterEncoding
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.
Inheritance
Initializers
init(destination:arrayEncoding:boolEncoding:)
Creates an instance using the specified parameters.
public init(destination: Destination = .methodDependent, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric)
Parameters
- destination:
Destinationdefining where the encoded query string will be applied..methodDependentby default. - arrayEncoding:
ArrayEncodingto use..bracketsby default. - boolEncoding:
BoolEncodingto use..numericby default.
Properties
`default`
Returns a default URLEncoding instance with a .methodDependent destination.
var `default`: URLEncoding
queryString
Returns a URLEncoding instance with a .queryString destination.
var queryString: URLEncoding
httpBody
Returns a URLEncoding instance with an .httpBody destination.
var httpBody: URLEncoding
destination
The destination defining where the encoded query string is to be applied to the URL request.
let destination: Destination
arrayEncoding
The encoding to use for Array parameters.
let arrayEncoding: ArrayEncoding
boolEncoding
The encoding to use for Bool parameters.
let boolEncoding: BoolEncoding
Methods
encode(_:with:)
public func encode(_ urlRequest: 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.
public func queryComponents(fromKey key: String, value: Any) -> [(String, String)]
Parameters
- key: Key of the query component.
- value: Value of the query component.
Returns
The percent-escaped, URL encoded query string components.
escape(_:)
Creates a percent-escaped string following RFC 3986 for a query string key or value.
public func escape(_ string: String) -> String
Parameters
- string:
Stringto be percent-escaped.
Returns
The percent-escaped String.