Take the 2-minute tour ×

I'm trying to test if a uri is valid (e.g. actually has content, not testing if it is well formed here) using ruby code, and I can open a uri using open(uri). But in my case, the uri is a link to a file to be downloaded and I don't want to have to download the whole file just to verify that there is content there.

Is there another solution for this?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Try this

require 'net/http'
u = URI.parse('http://www.example.com/')
status = Net::HTTP.start(u.host, u.port).head(u.request_uri).code
# status is HTTP status code

You'll need to use rescue to catch exception in case domain resolution fails.

share|improve this answer
Thanks, the head request seems to be what I was looking for. –  Jeff Storey Aug 13 '12 at 18:30
add comment

Your Answer


