Skip to content

Instantly share code, notes, and snippets.

@tb
Last active December 1, 2020 19:53
Show Gist options
  • Save tb/8787397 to your computer and use it in GitHub Desktop.
Save tb/8787397 to your computer and use it in GitHub Desktop.
Check if URL exists in Ruby
require 'net/http'
def working_url?(url_str)
url = URI.parse(url_str)
Net::HTTP.start(url.host, url.port) do |http|
http.head(url.request_uri).code == '200'
end
rescue
false
end
@wowremywang
Copy link

It doesn't work when checking if an image URL exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment