# Net/http set content-type for images

**URL:** <https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745>\
**Category:** Rails\
**Created:** [June 7, 2010, 2:42am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745 "2010-06-07T02:42:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 2:42am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/1 "2010-06-07T02:42:08Z")

</div>

I need to pull an image from a URL (I have permission) that is a .jpg.  
I know I need to set the content-type to ‘image/jpeg’, but not sure  
where to do that at.

result = Net::HTTP.get\_response(URI.parse(‘http://  
link\_to\_image.jpg’) )

result.body =\> gives me encoded junk.

Looking for help on how to set the content-type so I can display the  
image.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 3:38am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/2 "2010-06-07T03:38:55Z")

</div>

On Sun, Jun 6, 2010 at 5:40 PM, Kim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to pull an image from a URL (I have permission) that is a .jpg.

> result = Net::HTTP.get\_response(URI.parse(‘http://  
> link\_to\_image.jpg’) )
> 
> result.body =\> gives me encoded junk.

No, it gives you what you asked for: a JPEG-format IO stream.

Are you saving it, or passing it to some other process, or what?

> so I can display the image.

Or in other words, “display” it how?

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 5:18am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/3 "2010-06-07T05:18:59Z")

</div>

I am asking how to turn the JPEG IO stream into an image I can pass on  
and display in a view.

So I have

def index  
result = Net::HTTP.get\_response(URI.parse(‘http://  
link\_to\_image.jpg’) )  
…  
@image = ??

end

index.html.erb  
@image = pretty picture

On Jun 6, 6:37 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 5:53am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/4 "2010-06-07T05:53:31Z")

</div>

Sorry, I originally thought this was to the ruby list…

On Sun, Jun 6, 2010 at 8:17 PM, Kim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am asking how to turn the JPEG IO stream into an image I can pass on  
> and display in a view.

> def index  
> result = Net::HTTP.get\_response(URI.parse(‘http://  
> link\_to\_image.jpg’) )

> index.html.erb  
> @image = pretty picture

Uh, why not just use an image\_tag with the URL of the image?? Why  
are you even using Net::HTTP ?

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 8:06am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/5 "2010-06-07T08:06:58Z")

</div>

Really? Do you really think there isn’t a reason why I am not just  
using an image\_tag? Really?

The image is protected and I have been given access to pull it from my  
web server to use in my app.

Looking for help in answering my original post please. Thanks.

On Jun 6, 8:52 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 7, 2010, 10:08am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/6 "2010-06-07T10:08:09Z")

</div>

On Jun 7, 7:05 am, Kim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Really? Do you really think there isn’t a reason why I am not just  
> using an image\_tag? Really?

Seems like a legitimate question to me.

> The image is protected and I have been given access to pull it from my  
> web server to use in my app.
> 
> Looking for help in answering my original post please. Thanks.

All an image is is a stream of bytes, which you have already got.  
Furthermore the image is fetched by a separate request. If you really  
need to go down this route you should probably craft the ![]() tag to  
point at a particular controller/action in your app and from that  
action make your http request and return the bytes you get. You might  
also want to cache the image on your server.

Or perhaps some rewrite rules to proxy those requests to the origin  
server might make more sense

Fred

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [June 8, 2010, 2:07am UTC](https://www.ruby-forum.com/t/net-http-set-content-type-for-images/189745/7 "2010-06-08T02:07:09Z")

</div>

Thanks that did it. I am caching the image - though since it’s a  
webcam image that needs refreshed to stay current the cache is short  
lived.

def image  
@result = Net::HTTP.get\_response(URI.parse(‘http://  
link\_to\_image.jpg’) ).body  
end

index.html  
\<img src="webcam/image’\>

image.html.erb  
@result

On Jun 7, 1:07 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
