awesome-ios-developer/Helper/Network Layer/Encoding/JSONParameterEncoder.swift

23 lines
679 B
Swift
Raw Normal View History

2021-05-03 13:33:13 +07:00
//
// JSONParameterEncoder.swift
// MapDemoApp
//
// Created by JungpyoHong on 4/25/21.
//
import Foundation
2021-05-20 11:12:43 +07:00
struct JSONParameterEncoder: ParameterEncoder {
static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
2021-05-03 13:33:13 +07:00
do {
let jsonAsData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
urlRequest.httpBody = jsonAsData
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
}
2021-05-20 11:12:43 +07:00
} catch {
throw AppError.encodingFail
2021-05-03 13:33:13 +07:00
}
}
}