# Newbie - Test For JPEG Image Over HTTP

**URL:** <https://www.ruby-forum.com/t/newbie-test-for-jpeg-image-over-http/65284>\
**Category:** Ruby\
**Created:** [August 17, 2006, 10:17am UTC](https://www.ruby-forum.com/t/newbie-test-for-jpeg-image-over-http/65284 "2006-08-17T10:17:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 17, 2006, 10:17am UTC](https://www.ruby-forum.com/t/newbie-test-for-jpeg-image-over-http/65284/1 "2006-08-17T10:17:26Z")

</div>

Hi

I would like to test for the existence of a .jpg image on a remote  
server over HTTP.

I have tried 2 different methods but they are not what I need.

Method 1:

require ‘open-uri’  
if open(‘[http://example.net/path/to/image.jpg](http://example.net/path/to/image.jpg)’)

# do something

end

Method 2:

require ‘net/http’  
if Net::HTTP.get ‘[example.net](http://example.net)’, ‘/path/to/image.jpg’

# do something

end

The server where the images are always returns a default page if the  
images do not exist so the code above always returns.

What I need is a way to check what is returned is not a html page but is  
a .jpg image.

Any help greatly appeciated.  
Thanks  
Neil

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 17, 2006, 10:25am UTC](https://www.ruby-forum.com/t/newbie-test-for-jpeg-image-over-http/65284/2 "2006-08-17T10:25:50Z")

</div>

Neil Charlton wrote:

> if open(‘[http://example.net/path/to/image.jpg](http://example.net/path/to/image.jpg)’)  
> The server where the images are always returns a default page if the  
> images do not exist so the code above always returns.
> 
> What I need is a way to check what is returned is not a html page but is  
> a .jpg image.

Oh. Try this:

require ‘net/http’

def url\_is\_a\_jpg(url)  
u = URI.parse(url)  
h = Net::HTTP.new(u.host, u.port)  
r = h.head(u.path)  
return r.content\_type == ‘image/jpeg’  
end

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 17, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/newbie-test-for-jpeg-image-over-http/65284/3 "2006-08-17T12:38:42Z")

</div>

Alex Y. wrote:

> Neil Charlton wrote:
> 
> > if open(‘[http://example.net/path/to/image.jpg](http://example.net/path/to/image.jpg)’)  
> > The server where the images are always returns a default page if the  
> > images do not exist so the code above always returns.
> > 
> > What I need is a way to check what is returned is not a html page but is  
> > a .jpg image.
> 
> Oh. Try this:
> 
> require ‘net/http’
> 
> def url\_is\_a\_jpg(url)  
> u = URI.parse(url)  
> h = Net::HTTP.new(u.host, u.port)  
> r = h.head(u.path)  
> return r.content\_type == ‘image/jpeg’  
> end

Hi Alex

Thanks for your help. I can see what you are doing there unfortunately I  
get the following error

isjpg.rb:7:in `url_is_a_jpg': undefined method `content\_type’ for  
#\<Net::HTTPOK 200 readbody=true\> (NoMethodError)

ruby -v is  
ruby 1.8.2 (2004-12-25) [x86\_64-linux]

Any ideas ?

Thanks  
Neil
