以前在Request.authorizationHeader(..) 下的内容现在在HTTPHeaders.authorization(..) 下,为了更好地解释它,我将把它的变化代码放在这里:
在 this commit 之前我们在 Request.swift 中:
/// Returns a base64 encoded basic authentication credential as an authorization header tuple.
///
/// - parameter user: The user.
/// - parameter password: The password.
///
/// - returns: A tuple with Authorization header and credential value if encoding succeeds, `nil` otherwise.
open class func authorizationHeader(user: String, password: String) -> (key: String, value: String)? {
guard let data = "\(user):\(password)".data(using: .utf8) else { return nil }
let credential = data.base64EncodedString(options: [])
return (key: "Authorization", value: "Basic \(credential)")
}
由于 Alamofire 5 中的 this commit 我们可以在 HTTPHeaders.swift 中找到它:
/// Returns a `Basic` `Authorization` header using the `username` and `password` provided.
///
/// - Parameters:
/// - username: The username of the header.
/// - password: The password of the header.
///
/// - Returns: The header.
public static func authorization(username: String, password: String) -> HTTPHeader {
let credential = Data("\(username):\(password)".utf8).base64EncodedString()
return authorization("Basic \(credential)")
}
这意味着现在您应该能够通过以下方式做到这一点:
let headers: HTTPHeaders = [
.authorization(username: consumerKey!, password: consumerSecret!),
.accept("application/json")
]