mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-01-05 21:12:06 +07:00
23 lines
679 B
Swift
23 lines
679 B
Swift
//
|
|
// JSONParameterEncoder.swift
|
|
// MapDemoApp
|
|
//
|
|
// Created by JungpyoHong on 4/25/21.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct JSONParameterEncoder: ParameterEncoder {
|
|
static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
|
|
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")
|
|
}
|
|
} catch {
|
|
throw AppError.encodingFail
|
|
}
|
|
}
|
|
}
|