I'm using Alamofire to make my API calls.
Depending on the server I'm using the response headers could be capitalized.
But as the documentation says for allHeaderFields :
var allHeaderFields: [AnyHashable : Any] { get }
A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.
The keys in this dictionary are the header field names, as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.
HTTP headers are case insensitive. To simplify your code, certain header field names are canonicalized into their standard form. For example, if the server sends a content-length header, it is automatically adjusted to be Content-Length.
The returned dictionary of headers is configured to be case-preserving during the set operation (unless the key already exists with a different case), and case-insensitive when looking up keys.
For example, if you set the header X-foo, and then later set the header X-Foo, the dictionary’s key will be X-foo, but the value will taken from the X-Foo header.
But in my code if I'm doing this :
if let headers = response.response?.allHeaderFields {
print("Access-Token: \(response.response?.allHeaderFields["Access-Token"])")
print("access-token: \(response.response?.allHeaderFields["access-token"])")
print("access-token: \(response.response?.allHeaderFields["Access-token"])")
}
In the console I have
Access-Token: nil
access-token: Optional(jdRtDzKHNs_i-jt3Lh3a3A)
access-token: nil
Am I missing something ?
The bug is not related with Alamofire. This is a Swift bug.
Unfortunately this is a known bug that appeared at Swift 3 when they transformed the headers into Dictionary. The bug is registered since 2016 and remains unsolved. To make things worst they not even corrected the Swift documentation.
This is a violation of the HTTP specification and I have no idea why they flagged this only as a Medium priority. Seems that they do not care and will never solve this problem.