mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-01-05 21:12:06 +07:00
28 lines
971 B
Swift
28 lines
971 B
Swift
//
|
|
// URLParameterEncoder.swift
|
|
// MapDemoApp
|
|
//
|
|
// Created by JungpyoHong on 4/25/21.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct URLParameterEncoder: ParameterEncoder {
|
|
static func encode(urlRequest: inout URLRequest, with parameters: Parameters) throws {
|
|
guard let url = urlRequest.url else { throw AppError.badUrl }
|
|
|
|
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
|
|
urlComponents.queryItems = [URLQueryItem]()
|
|
|
|
for (key, value) in parameters {
|
|
let queryItem = URLQueryItem(name: key, value: "\(value)")
|
|
urlComponents.queryItems?.append(queryItem)
|
|
}
|
|
urlRequest.url = urlComponents.url
|
|
}
|
|
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
|
|
urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
|
|
}
|
|
}
|
|
}
|