Sabbir Ahmed
Sabbir Ahmed

Reputation: 349

Alamofire 5 type 'Request' has no member 'authorizationHeader'

After updating to Alamofire 5 "Request.authorizationHeader(user: String, password: String)" method show error.

Error:- Type 'Request' has no member 'authorizationHeader'

Code:

    // Safely unwrap token
    guard let safeHeader = Request.authorizationHeader(user: consumerKey!, password: consumerSecret!) else {
        return nil
    }

Upvotes: 3

Views: 1067

Answers (1)

denis_lor
denis_lor

Reputation: 6547

What used to be under Request.authorizationHeader(..) is now under HTTPHeaders.authorization(..), to better explain it I'll put here the code how it has changed:

Before this commit we had in 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)")
}

Since this commit in Alamofire 5 we can find it within 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)")
}

That means now you should be able to do the same by doing:

let headers: HTTPHeaders = [
        .authorization(username: consumerKey!, password: consumerSecret!),
        .accept("application/json")
    ]

Upvotes: 6

Related Questions