# Checking URL with open-uri

**URL:** <https://www.ruby-forum.com/t/checking-url-with-open-uri/142533>\
**Category:** Rails\
**Created:** [July 8, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533 "2008-07-08T23:19:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [July 8, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533/1 "2008-07-08T23:19:12Z")

</div>

Hi,

I allow posting videos to youtube in my app. But before I let them save  
into the DB I want to, at least, know that the users put a link to  
youtube and that the link is working.

How can I do those verifications with open-uri?

Tried but got some errors…

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [July 9, 2008, 12:32am UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533/2 "2008-07-09T00:32:06Z")

</div>

Using ‘net/http’ you can:

url = ‘[http://www.youtube.com/foobar-blarg](http://www.youtube.com/foobar-blarg)’  
begin  
result = Net::HTTP.get\_response(URI.parse(url))  
rescue  
puts ‘Found an error’ # eg cant connect to domain  
exit  
end  
puts ‘Not found’ if result.is\_a?(Net::HTTPNotFound)

2008/7/9 comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [July 9, 2008, 7:26am UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533/3 "2008-07-09T07:26:37Z")

</div>

Thanks Phil!

Then I can find the youtube from the uri and exit if not from that site.

I have tested your suggested code and succesfully gave me “found an  
error” when I entered nonsense as url.

But then I also got “found an error” when the url was ok or non  
existent. The controller spend around 30 seconds and then gave me the  
same error. I just copy pasted, I must be doing something wrong…

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [July 10, 2008, 7:33am UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533/4 "2008-07-10T07:33:53Z")

</div>

Hi,

I fixed the problem I needed either to add a proxy or missed a require  
8-P

Cheers

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 19, 2009, 11:20am UTC](https://www.ruby-forum.com/t/checking-url-with-open-uri/142533/5 "2009-06-19T11:20:27Z")

</div>

comopasta Gr wrote:

> Hi,
> 
> I fixed the problem I needed either to add a proxy or missed a require  
> 8-P
> 
> Cheers

can write the detail code ? ,thanks
