我需要将一些参数放入查询字符串,将一些 json 放入 htpbody
所以我有这样的东西
public func asURLRequest() throws -> URLRequest
{
let combined = baseUrl.absoluteString + path
guard let url_ = URL(string: combined) else {
print ("invalid url \(combined)")
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
guard var components = URLComponents(url: url_, resolvingAgainstBaseURL: false) else {
print ("invalid url \(url_)")
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
// var items: [URLQueryItem] = components.queryItems ?? []
// for (key, value) in query_parameters ?? [:]{
// guard let value = value as? String else {
// assertionFailure()
// continue
// }
// let item = URLQueryItem(name: key, value: value)
// items.append(item)
// }
// components.queryItems = items
guard let url = components.url else {
print ("invalid compontns \(components)")
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
let originalRequest: URLRequest = try URLRequest(url: url, method: method, headers: headers)
let params: Parameters = parameters
let uRLRequest = try parameterEncoding.encode(originalRequest, with: params)
if let query_parameters = query_parameters {
let encodedURLRequest = try URLEncoding.default.encode(uRLRequest, with: query_parameters)
return encodedURLRequest
}
return uRLRequest
}
但查询参数未编码:
https://somehostsomewhere/imobile/LoyaltyProxy?language=ru&path=v1/user_app/user/balance
路径中有斜线:-[
这里的讨论https://github.com/Alamofire/Alamofire/issues/374已于 2017 年死亡
我想弄错了,但我怀疑将来会有 alamofire 6 和 7 和 8 的线程