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
. Since there is no published specification for how to encode
collection types, 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
).
-
Defines whether the url-encoded query string is applied to the existing query string or HTTP body of the resulting URL request.
- methodDependent: Applies encoded query string result to existing query string for
GET
,HEAD
andDELETE
requests and sets as the HTTP body for requests with any other HTTP method. - queryString: Sets or appends encoded query string result to existing query string.
- httpBody: Sets encoded query string result as the HTTP body of the URL request.
Declaration
Swift
public enum Destination
- methodDependent: Applies encoded query string result to existing query string for
-
Returns a default
URLEncoding
instance.Declaration
Swift
public static var `default`: URLEncoding
-
Returns a
URLEncoding
instance with a.methodDependent
destination.Declaration
Swift
public static var methodDependent: URLEncoding
-
Returns a
URLEncoding
instance with a.queryString
destination.Declaration
Swift
public static var queryString: URLEncoding
-
Returns a
URLEncoding
instance with an.httpBody
destination.Declaration
Swift
public static var httpBody: URLEncoding
-
The destination defining where the encoded query string is to be applied to the URL request.
Declaration
Swift
public let destination: Destination
-
Creates a
URLEncoding
instance using the specified destination.Declaration
Swift
public init(destination: Destination = .methodDependent)
-
Creates a URL request by encoding parameters and applying them onto an existing request.
Throws
An
Error
if the encoding process encounters an error.Declaration
Swift
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
-
Creates percent-escaped, URL encoded query string components from the given key-value pair using recursion.
Declaration
Swift
public func queryComponents(fromKey key: String, value: Any) -> [(String, String)]
-
Returns a percent-escaped string following RFC 3986 for a query string key or value.
RFC 3986 states that the following characters are
reserved
characters.- General Delimiters:
:
,#
,[
,]
,@
,?
,/
- Sub-Delimiters:
!
,$
,&
,’
,(
,)
,*
,+
,,
,;
,=
In RFC 3986 - Section 3.4, it states that the
?
and/
characters should not be escaped to allow query strings to include a URL. Therefore, allreserved
characters with the exception of?
and/
should be percent-escaped in the query string.Declaration
Swift
public func escape(_ string: String) -> String
- General Delimiters: