Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

http

ruby

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?

like image 816
martini-bonanza Avatar asked May 09 '10 11:05

martini-bonanza


People also ask

Why do I keep getting error code 404?

404 error codes are generated when a user attempts to access a webpage that does not exist, has been moved, or has a dead or broken link. The 404 error code is one of the most frequent errors a web user encounters. Servers are required to respond to client requests, such as when a user attempts to visit a webpage.

What is 404 error asp net?

Best web development and SEO practices dictate that any webpage which does not exist, return an HTTP response code of 404, or Not Found. Basically, this response code means that the URL that you're requesting does not exist.


2 Answers

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.

like image 161
Theo Avatar answered Sep 28 '22 19:09

Theo


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

like image 45
kristianp Avatar answered Sep 28 '22 17:09

kristianp