27 lines
848 B
Swift
27 lines
848 B
Swift
//
|
|
// JSONParameterEncoder.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Jura on 8/21/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct JSONParameterEncoder: ParameterEncoder {
|
|
public static func encode(parameters: HTTPParameters, for request: inout URLRequest) throws {
|
|
|
|
guard let jsonData = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {
|
|
throw NetworkRouterError.encoding(params: parameters)
|
|
}
|
|
|
|
request.httpBody = jsonData
|
|
if request.value(forHTTPHeaderField: "Content-Type").isExist {
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
} else {
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
}
|
|
|
|
}
|
|
}
|