Ghost
Ghost

Reputation: 70

Error request Alamofire + Basic Auth

I'm trying to make this request but only returns error 401, is the request correct? I have to use Basic Auth

    var user = ""
    var password = ""

    user = textField.text!
    password = textField2.text!

    print(user)
    print(password)


    let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
    print(credentialData)
    let base64Credentials = credentialData.base64EncodedString(options: [])
    print(base64Credentials)
    let headers: HTTPHeaders = ["Authorization":" Basic \(base64Credentials)"]

    print(headers)

    Alamofire.request("https://www.floratilemevidencia.com.br/wp-json/wp/v2/users/me", headers: headers)
        .validate().responseJSON { response in
            switch response.result {

            case .success:
                print("Validation Successful")
                let viewController: UIViewController = self.storyboard!.instantiateViewController(withIdentifier: "SideNavigationController")
                self.present(viewController, animated: true, completion: { _ in })

            case .failure(let error):
                print(error.localizedDescription)
                self.alertLabel.isHidden = false
            }
    }

It only returns me error 401.

It's correct part of HTTPHeaders?

Upvotes: 1

Views: 1053

Answers (2)

H S W
H S W

Reputation: 7119

Make small changes in your code and it will work.

var user = ""
var password = ""

user = textField.text!
password = textField2.text!

print(user)
print(password)


let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
print(credentialData)

let base64Credentials = credentialData.base64EncodedString(options: [])
print(base64Credentials)

let url: String = "https://www.floratilemevidencia.com.br/wp-json/wp/v2/users/me"

var request = URLRequest(url:  NSURL(string: url)! as URL)
request.httpMethod = "GET"
request.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")

Alamofire.request(request)
    .validate().responseJSON { response in
        switch response.result {

        case .success:
            print("Validation Successful")
            // Your desired functionality

        case .failure(let error):
            print(error.localizedDescription)
            // Your desired functionality

        }
}

Upvotes: 1

dmorrow
dmorrow

Reputation: 5664

Better to let Alamofire generate your header. Per https://github.com/Alamofire/Alamofire#authentication

EDIT

replace this block of code

let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
print(credentialData)
let base64Credentials = credentialData.base64EncodedString(options: [])
print(base64Credentials)
let headers: HTTPHeaders = ["Authorization":" Basic \(base64Credentials)"]

with

var headers: HTTPHeaders = [:]

if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
    headers[authorizationHeader.key] = authorizationHeader.value
}

Upvotes: 0

Related Questions