How to handle 404 errors with Ruby HTTP::Net?

10,010

Solution 1

Rewrite your code like this:

uri = URI.parse(url)
result = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) }
puts result.code
puts result.body

That will print the status code followed by the body.

Solution 2

As you know, your code will always return the response body, whether there is an error or not. In order to test the response code, use Theo's answer, and the following if statement, for example:

if result.code.to_i < 400
  puts "success"
end

This example converts the code (which is a string) to an integer, and treats redirects and various 200 codes as successful.

See this for the various codes returned: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Share:
10,010
martini-bonanza
Author by

martini-bonanza

Updated on June 13, 2022

Comments

  • martini-bonanza
    martini-bonanza almost 2 years

    I'm trying to parse web pages but I sometimes get 404 errors. Here's the code I use to get the web page:

    result = Net::HTTP::get URI.parse(URI.escape(url))
    

    How do I test if result is a 404 error code?

  • martini-bonanza
    martini-bonanza almost 14 years
    Thanks! I tried this but result.status didn't work. Instead, I checked result.class.name == "HTTPOK", it does the trick :-)
  • Theo
    Theo almost 14 years
    Sorry, it should have been result.code, not result.status. I've updated my answer.
  • Veck Hsiao
    Veck Hsiao over 8 years
    Note: URI is in module net/http