http - (Swift) How to check if content-encoding is gzip -
is there way in swift find out if data request returns in gzip?
i want write test method check if content-encoding returns gzip file:
you can info header fields in httpurlresponse
:
urlsession.shared.datatask(with: url) { (data, response, error) in if let response = response as? httpurlresponse { if let encoding = response.allheaderfields["content-encoding"] as? string { print(encoding) print(encoding == "gzip") } } }.resume()
note downloads headers and data.
if want headers without downloading data, better solution use urlrequest
set "head"
this:
var request = urlrequest(url: url) request.httpmethod = "head" urlsession.shared.datatask(with: request) { (_, response, _) in if let response = response as? httpurlresponse { if let enc = response.allheaderfields["content-encoding"] as? string { print(enc) print(enc == "gzip") } } }.resume()
this way, headers downloaded.
Comments
Post a Comment