如何在带有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)
}
答案 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)