JSONEncoding
public struct JSONEncoding : ParameterEncoding
Uses JSONSerialization
to create a JSON representation of the parameters object, which is set as the body of the
request. The Content-Type
HTTP header field of an encoded request is set to application/json
.
-
Error produced by
See moreJSONEncoding
.Declaration
Swift
public enum Error : Swift.Error
-
Returns a
JSONEncoding
instance with default writing options.Declaration
Swift
public static var `default`: JSONEncoding { get }
-
Returns a
JSONEncoding
instance with.prettyPrinted
writing options.Declaration
Swift
public static var prettyPrinted: JSONEncoding { get }
-
The options for writing the parameters as JSON data.
Declaration
Swift
public let options: JSONSerialization.WritingOptions
-
Creates an instance using the specified
WritingOptions
.Declaration
Swift
public init(options: JSONSerialization.WritingOptions = [])
Parameters
options
JSONSerialization.WritingOptions
to use.
-
Declaration
Swift
public func encode(_ urlRequest: any URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
-
Encodes any JSON compatible object into a
URLRequest
.Throws
Any
Error
produced during encoding.Declaration
Swift
public func encode(_ urlRequest: any URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest
Parameters
urlRequest
URLRequestConvertible
value into which the object will be encoded.jsonObject
Any
value (must be JSON compatible) to be encoded into theURLRequest
.nil
by default.Return Value
The encoded
URLRequest
.