How to retrieve response headers with Alamofire in Swift?

Posted by Wei on May 16, 2017

Question

I’m using Alamofire to send a request to API, and API will send a reponse with headers. How can I retrive headers from it.

Answer

The answer below is using Swift 3.

Alamofire.request(.GET, requestUrl, parameters:parameters, headers: headers).responseJSON {
        response in
        print(response.response?.allHeaderFields)
}

response.response?.allHeaderFields contains all headers from the reponse.

If you want to get a certain value from headers, use response.response?.allHeaderFields[value] to get that header.

Reference


This is the end of post