How to get response headers when using Alamofire in Swift?

I use Alamofire for my Rest Request (POST) and get a JSON response without any problems. But I can only access the body of the response. I want to get response headers. Is it impossible to use Alamofire?

Here is my code snippet:

@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)" } } } 
+8
ios iphone swift alamofire
source share
2 answers

Since the response is of type NSHTTPURLResponse , you should be able to get the headers like this:

 response.allHeaderFields 
+24
source share

Here's how to access answer headers in Swift 3:

 Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers) .responseJSON { response in if let headers = response.response?.allHeaderFields as? [String: String]{ let header = headers["token"] // ... } } 
+3
source share

All Articles