Back to Alamofire

JSONEncoding

docs/docsets/Alamofire.docset/Contents/Resources/Documents/Structs/JSONEncoding.html

5.12.02.4 KB
Original Source

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
                `

Error produced by JSONEncoding.

See more

Declaration

Swift

public enum Error : Swift.Error

Properties

`

                default
                `

Returns a JSONEncoding instance with default writing options.

Declaration

Swift

public static var `default`: JSONEncoding { get }

`

                prettyPrinted
                `

Returns a JSONEncoding instance with .prettyPrinted writing options.

Declaration

Swift

public static var prettyPrinted: JSONEncoding { get }

`

                options
                `

The options for writing the parameters as JSON data.

Declaration

Swift

public let options: JSONSerialization.WritingOptions

Initialization

`

                init(options:)
                `

Creates an instance using the specified WritingOptions.

Declaration

Swift

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

Parameters

| options |

JSONSerialization.WritingOptions to use.

|

Encoding

`

                encode(_:with:)
                `

Declaration

Swift

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

`

                encode(_:withJSONObject:)
                `

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.