# Check whether externam URL is valid?

**URL:** <https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878>\
**Category:** Rails\
**Created:** [March 14, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878 "2006-03-14T23:32:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 14, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878/1 "2006-03-14T23:32:10Z")

</div>

Hey,

I have users entering URLs into my app, is there anyway in which I can  
check to see if the URLs actually point to something before saving the  
user input? Maybe a kind of ‘ping’-action that returns true on success?

(btw: does anyone of a tentative release date for Rails1.1? I’ve been  
slobbering all over my RailsRecipes book and I can’t wait! Also, Scott  
Raymond’s compiled a list of awesome stuff that’ll be incorporated into  
the next release. . . link on [LoudThinking.Com](http://LoudThinking.Com))

Lots of Thanks,  
Gustav  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 14, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878/2 "2006-03-14T23:33:14Z")

</div>

mmm…Externam should read External 🙂 'suppose you noticed.

Thanks,  
Gustav

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 14, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878/3 "2006-03-14T23:48:32Z")

</div>

Net::HTTP looks like your best shot.

require ‘net/http’  
Net::HTTP.start(‘[www.something.com](http://www.something.com)’) do |http|  
response = http.get(‘/index.html’)  
p response.code  
end

Check acceptable response codes here: [HTTP - Hypertext Transfer Protocol Overview](http://www.w3.org/Protocols/)  
rfc2616/rfc2616-sec10.html

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 15, 2006, 5:21am UTC](https://www.ruby-forum.com/t/check-whether-externam-url-is-valid/54878/4 "2006-03-15T05:21:59Z")

</div>

Hey John,

That only works for valid URL’s. It doesn’t alert you if the URL isn’t  
there. I’m not sure how to check that?

I’ve tried your code with a check on the response.

unless response == ‘200’  
p ‘Site is down’  
else  
p ‘Found it!’  
end

I get a “SocketError: getaddrinfo: No address associated with  
nodename”, which isn’t checkable to my knowledge.

Any ideas?  
Thanks,  
Clint
