我有一个问题:我使用Alamofire从我的API成功检索了一个令牌,它是一个字符串。我想要做的是:“获取令牌(字符串)并将其放入另一个请求以从API获取一些JSON数据”
CURL: 卷曲-X GET -H“授权:令牌5f1884848”http://myurl.com
这就是我得到令牌的原因:
let parameters = [
"username" : usernameLabel.text!,
"password" : passwordLabel.text!
]
let credentialData = "\(usernameLabel.text):\(passwordLabel.text)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.POST, requestString, parameters: parameters, encoding: .JSON, headers: headers)
.responseJSON { response in switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
let response = JSON as! NSDictionary
//example if there is a token
self.userToken = response.objectForKey("token") as! String?
print(self.userToken)
debugPrint(response)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
我试图以某种方式保存userToken变量:
class ViewController: UIViewController {
var userToken : String?
但我的userToken var始终为nil