Skip to content

Instantly share code, notes, and snippets.

@tmspzz
Last active October 24, 2017 10:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmspzz/923f1c1de2f00514ed12 to your computer and use it in GitHub Desktop.
Save tmspzz/923f1c1de2f00514ed12 to your computer and use it in GitHub Desktop.
Alamofire-GZIP-ParameterEncoding
// Actual gzipping from https://github.com/1024jp/NSData-GZIP
// Example: ParameterEncoding.JSON.gzipped
infix operator • { associativity left }
func • <A, B, C>(f: B -> C, g: A -> B) -> A -> C {
return { x in f(g(x)) }
}
extension ParameterEncoding {
var gzipped:ParameterEncoding {
return gzip(self)
}
private func gzip(encoding:ParameterEncoding) -> ParameterEncoding {
let gzipEncoding = self.gzipOrError • encoding.encode
return ParameterEncoding.Custom(gzipEncoding)
}
private func gzipOrError(request:NSURLRequest, error:NSError?) -> (NSMutableURLRequest, NSError?) {
let mutableRequest = request.mutableCopy() as! NSMutableURLRequest
if error != nil {
return (mutableRequest, error)
}
var gzipEncodingError: NSError? = nil
do {
let gzippedData = try mutableRequest.HTTPBody?.gzippedData()
mutableRequest.HTTPBody = gzippedData
if mutableRequest.HTTPBody != nil {
mutableRequest.setValue("gzip", forHTTPHeaderField: "Content-Encoding")
}
} catch {
gzipEncodingError = error as NSError
}
return (mutableRequest, gzipEncodingError)
}
}
@cristov26
Copy link

Can you update this to the last version of alamofire?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment