Skip to content

Instantly share code, notes, and snippets.

@schneems
Created January 30, 2012 17:09
Show Gist options
  • Save schneems/1705468 to your computer and use it in GitHub Desktop.
Save schneems/1705468 to your computer and use it in GitHub Desktop.
Receive GZIP params in Rails
def handle_gzip_params
# When the server receives a request with content-type "gzip/json" this will be called which will unzip it,
# and then parse it as json
# The use case is so clients such as Android or Iphone can zip their long request such as Inviters#addressbook emails
# Then the server can unpack the request and parse the parameters as normal.
if request.content_type == "gzip/json"
data = ActiveSupport::JSON.decode(ActiveSupport::Gzip.decompress(request.raw_post))
data = {:_json => data} unless data.is_a?(Hash)
params ||= {}
self.params.merge!(data.to_options) #params.merge(data.with_indifferent_access).inject({}){|temp,(k,v)| temp[k.to_sym] = v; temp} # replace string keys with symbols
end
end
@plentz
Copy link

plentz commented Mar 12, 2013

isn't easier to just use use Rack::Deflater?

@jodigiordano
Copy link

Rack::Deflater is used to compress http responses, not receive compressed http requests.

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