Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Handling gzip responses in Ruby Net::HTTP library
# from
# i wanted syntax highlighting
require 'net/http'
debug ={|msg| STDERR.puts "[#{'%Y-%m-%d %H:%M:%S')}] #{msg}" }
page = nil
http = "", 80 )
req = "/search?num=20&hl=en&noj=1&q=test&btnG=Search", { "Accept-Encoding" => "gzip", "User-Agent" => "gzip" } ) "Performing HTTP GET request for (#{req.path})." )
res = http.request( req ) "Received HTTP Response Code (#{res.code})" )
case res
when Net::HTTPSuccess then
if res.header[ 'Content-Encoding' ].eql?( 'gzip' ) then "Performing gzip decompression for response body." )
sio = res.body )
gz = sio )
page = "Finished decompressing gzipped response body." )
else "Page is not compressed. Using text response body. " )
page = res.body
rescue Exception "Error occurred (#{$!.message})" )
# handle errors
raise $!.message
puts page

This comment has been minimized.

Copy link

@otobrglez otobrglez commented May 4, 2014

Have in mind that Net::HTTP also supports compression out of the box.

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