在Swift中使用Alamofire时如何获取响应头? [英] How to get response headers when using Alamofire in Swift?
本文介绍了在Swift中使用Alamofire时如何获取响应头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Alamofire 获取我的Rest(POST)请求并无缝获取JSON响应。但我只能访问响应机构。我想获得响应标头。使用Alamofire时难道不可能吗?
I'm using Alamofire for my Rest (POST) request and getting JSON response seamlessly. But i can access only response body. I want to get response headers. Isn't it possible when using Alamofire?
这是我的代码片段:
@IBAction func loginButtonPressed(sender: UIButton) {
let baseUrl = Globals.ApiConstants.baseUrl
let endPoint = Globals.ApiConstants.EndPoints.authorize
let parameters = [
"apikey": "api_key_is_here",
"apipass": "api_pass_is_here",
"agent": "agent_is_here"
]
Alamofire.request(.POST, baseUrl + endPoint, parameters: parameters).responseJSON {
(request, response, data, error) in let json = JSON(data!)
if let result = json["result"].bool {
self.lblResult.text = "result: \(result)"
}
}
}
推荐答案
由于回复是 NSHTTPURLResponse
类型,您应该能够获得如下所示的标题:
As response is of NSHTTPURLResponse
type, you should be able to get the headers as followed:
response.allHeaderFields
这篇关于在Swift中使用Alamofire时如何获取响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文