Last active
November 18, 2019 13:54
-
-
Save abekert/af7934ea7d24d1269d0270d6770136c6 to your computer and use it in GitHub Desktop.
Get header value by a case insensitive key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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