Search code examples
xcode8alamofireswifty-json

How to read the headers of the REST API response using TRON and Alamofire


I am currently using Xcode8 and have built an app that make REST API calls using TRON and Alamofire.

I am successfully calling the API and getting back a response. This response BODY is being parsed into a model class that is structured to mimic the response body of the API call, and all of my properties are being populated with the correct values.

The new requirement I have now been given is to read some authentication information from the HEADER of the response. This information has been defined as a JSON Web Token. I can't seem to find any information on how to parse this information from the response HEADER using TRON or Alamofire.

Any insight, example, links, or comments will be greatly appreciated. Thanks in advance for any help.


Solution

  • I found the answer on this SO post.

    And then I solved it in my project like this:

    if let authorization = response.response?.allHeaderFields["Authorization"] as? String {
                            UserDefaults.standard.set(authorization, forKey: Constant.AUTHORIZATION_TOKEN)
                        }