Skip to content

Instantly share code, notes, and snippets.

@abekert
Last active November 18, 2019 13:54
Show Gist options
  • Save abekert/af7934ea7d24d1269d0270d6770136c6 to your computer and use it in GitHub Desktop.
Save abekert/af7934ea7d24d1269d0270d6770136c6 to your computer and use it in GitHub Desktop.
Get header value by a case insensitive key
import Foundation
public extension HTTPURLResponse {
/// Return http header value for case insensitive `headerField` key
///
/// Why do we need this?
/// Well, there is a bug, which leads `allHeaderFields` dictionary to be case sensitive.
/// https://bugs.swift.org/browse/SR-2429
/// Workaround with casting to `NSDictionary` is still working, but Apple introduced a new API to get header value by a key
/// https://developer.apple.com/documentation/foundation/httpurlresponse/3240613-value
func valueForHeaderField(_ headerField: String) -> String? {
if #available(iOS 13.0, *) {
return value(forHTTPHeaderField: headerField)
} else {
return (allHeaderFields as NSDictionary)[headerField] as? String
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment