Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created October 15, 2012 13:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romanbsd/3892387 to your computer and use it in GitHub Desktop.
Save romanbsd/3892387 to your computer and use it in GitHub Desktop.
Faraday gzip response middleware
require 'faraday'
require 'zlib'
module FaradayMiddleware
class Gzip < Faraday::Response::Middleware
def on_complete(env)
encoding = env[:response_headers]['content-encoding'].to_s.downcase
case encoding
when 'gzip'
env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body]), encoding: 'ASCII-8BIT').read
env[:response_headers].delete('content-encoding')
when 'deflate'
env[:body] = Zlib::Inflate.inflate(env[:body])
env[:response_headers].delete('content-encoding')
end
end
end
end
Faraday::Response.register_middleware :gzip => FaradayMiddleware::Gzip
@spiegela
Copy link

spiegela commented Mar 8, 2013

This looks good. Are you going to submit a pull-request to faraday_middleware?

@meatballhat
Copy link

👍 especially since it's on the roadmap wiki page!

@ismasan
Copy link

ismasan commented Dec 12, 2013

Should this not send an Accept-Encoding request header?

@romanbsd
Copy link
Author

Yeah, good point. I'm setting the request headers myself. But for this to be feature complete, I need to add it to middleware. And then it should be added with :use rather then :response

@romanbsd
Copy link
Author

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