Getting the contents of a 404 error page response ruby

nokogiri, open-uri, ruby

Solution

This doesn't seem to be stated clearly enough in the docs, but HttpError has an io attribute, which you can treat as a read only file as far as i know.

require 'open-uri'

begin
  response = open('http://google.com/blahblah')
rescue => e
  puts e # Error message
  puts e.io.status # Http Error code
  puts e.io.readlines # Http response body
end

Problem

I know some languages have a library that allows you to get the HTTP content for a 404 or 500 message. Is there a library that allows that for Ruby? I've tried open-uri but it simply returns an HTTPError exception without the HTML content for the 404 response.

Original source