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.

Properties

  • 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

Initialization

  • Creates an instance using the specified WritingOptions.

    Declaration

    Swift

    public init(options: JSONSerialization.WritingOptions = [])

    Parameters

    options

    JSONSerialization.WritingOptions to use.

Encoding

  • 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 the URLRequest. nil by default.

    Return Value

    The encoded URLRequest.