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 a JSONEncoding instance using the specified options.

    Declaration

    Swift

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

    Parameters

    options

    The options for writing the parameters as JSON data.

    Return Value

    The new JSONEncoding instance.

Encoding

  • 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

    Parameters

    urlRequest

    The request to have parameters applied.

    parameters

    The parameters to apply.

    Return Value

    The encoded request.

  • Creates a URL request by encoding the JSON object and setting the resulting data on the HTTP body.

    Throws

    An Error if the encoding process encounters an error.

    Declaration

    Swift

    public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest

    Parameters

    urlRequest

    The request to apply the JSON object to.

    jsonObject

    The JSON object to apply to the request.

    Return Value

    The encoded request.