如何将基本身份验证添加到Swift 2.2 Alamofire3 API请求,以便对Drupal 7进行REST调用?

时间:2016-07-19 23:21:45

标签: rest basic-authentication

如何在带有Alamofire 3的Swift 2.2中为此API请求添加Basic Auth?

这是我的代码:

    let voteEndpoint: String = "https://www.example.com/rest/votingapi/set_votes"
    let newVote = ["votes":["value":40, "uid":1, "entity_id":108]]
    Alamofire.request(.POST, voteEndpoint, parameters: newVote, encoding: .JSON)

        .response { request, response, data, error in
            print(request)
            print (response)
            print (error)
    }

1 个答案:

答案 0 :(得分:1)

根据文档试试这个。

let user = ""
let password = ""
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.POST, voteEndpoint, parameters: newVote, headers: headers, encoding: .JSON)