【发布时间】:2020-10-07 01:27:41
【问题描述】:
最近,我将旧的 Alamofire 请求替换为 URLRequestConvertible 协议,例如,国家/地区路由是这样的:
enum CountryRoute: URLRequestConvertible {
case countries
var path: String {
switch self {
case .countries : return Path.Common.countries.rawValue
}
}
var method: HTTPMethod {
return .get
}
public var headers: HTTPHeaders {
return ["X-Mode" : "Light"]
}
func asURLRequest() throws -> URLRequest {
let url = try "example.com".asURL()
var request = URLRequest(url: url.appendingPathComponent(path))
request.httpMethod = method.rawValue
request.headers = headers
request.timeoutInterval = TimeInterval(10*1000)
return try URLEncoding.default.encode(request,with: nil)
}
}
最后的请求是这样的:
shared.session.request(convertible).validate().responseData { (response) in
// Do something
}
据我了解,我们为每个路由设置了标头,但是如果我们有像 "Content-Type" 或 "Accept-Language" 甚至 这样的全局标头怎么办>不记名令牌?
有什么方法可以防止添加重复的标题并在全局范围内添加它们?还是我们必须将这些标头添加到每个路由?
【问题讨论】:
标签: swift http-headers alamofire