JSONEncoding - SwiftDocOrg/Alamofire GitHub Wiki

JSONEncoding

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.

public struct JSONEncoding:โ€‹ ParameterEncoding

Inheritance

ParameterEncoding

Initializers

init(options:โ€‹)

Creates an instance using the specified WritingOptions.

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

Parameters

  • options:โ€‹ JSONSerialization.WritingOptions to use.

Properties

`default`

Returns a JSONEncoding instance with default writing options.

var `default`:โ€‹ JSONEncoding

prettyPrinted

Returns a JSONEncoding instance with .prettyPrinted writing options.

var prettyPrinted:โ€‹ JSONEncoding

options

The options for writing the parameters as JSON data.

let options:โ€‹ JSONSerialization.WritingOptions

Methods

encode(_:โ€‹with:โ€‹)

public func encode(_ urlRequest:โ€‹ URLRequestConvertible, with parameters:โ€‹ Parameters?) throws -> URLRequest

encode(_:โ€‹withJSONObject:โ€‹)

Encodes any JSON compatible object into a URLRequest.

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

Parameters

  • urlRequest:โ€‹ URLRequestConvertible value into which the object will be encoded.
  • jsonObject:โ€‹ Any value (must be JSON compatibleto be encoded into theURLRequest. nil` by default.

Throws

Any Error produced during encoding.

Returns

The encoded URLRequest.