我尝试使用Swift进行身份验证请求。用户登录时,将使用以下内容设置经过身份验证的标头:
let user = userEmail
let password = userPassword
let plainString = "\(user):\(password)" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let manager = Alamofire.Manager.sharedInstance
在另一个控制器中,我尝试进行经过身份验证的请求,但它不起作用,这里是代码:
let manager = Alamofire.Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = defaultHeaders
manager.request(.GET, "url")
.responseJSON {(request, response, data, error) in
}
我收到404错误,因为未在标头中设置授权。有任何想法吗?
答案 0 :(得分:0)
这应该有效:
let headers = [
// write your header here
]
Alamofire.request(.GET, url, parameters: nil, encoding: .URL, headers: headers).response { (request, response, data, error) -> Void in
}