[英]How to pass token value with Alamofire
我使用 Alamofire 從我的 API 中成功檢索了一個令牌,它是一個字符串。 我想獲取令牌(字符串)並將其放入另一個請求中以從 API 獲取一些 JSON 數據。 但我不知道如何通過它。
var token = String()// global variable
let parameters = [
"username" : usernameLabel.text!,
"password" : passwordLabel.text!
]
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
token = response.objectForKey("token") as! String?
print(token)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
嘗試撥打電話:
var token = String()// global variable
let parameters = [
"username" : usernameLabel.text!,
"password" : passwordLabel.text!
]
let headers = [
"Authorization" : String(format: "Bearer: @%", token)
]
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
token = response.objectForKey("token") as! String?
print(token)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.