0

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?

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

1 Answers1

3

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.

Kulbir Saini
  • 3,926
  • 1
  • 26
  • 34