Update JSONParameterEncoder.swift

This commit is contained in:
Jungpyo Hong 2021-05-19 23:12:43 -05:00 committed by GitHub
parent 561bb7d619
commit 21af3d0920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,16 +7,16 @@
import Foundation import Foundation
public struct JSONParameterEncoder: ParameterEncoder { struct JSONParameterEncoder: ParameterEncoder {
public func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws { static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
do { do {
let jsonAsData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) let jsonAsData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
urlRequest.httpBody = jsonAsData urlRequest.httpBody = jsonAsData
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil { if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
} }
}catch { } catch {
throw NetworkError.encodingFailed throw AppError.encodingFail
} }
} }
} }