# Respond to REST-request with image-file (no XML etc.)

**URL:** <https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166>\
**Category:** Rails\
**Created:** [January 6, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166 "2008-01-06T15:02:34Z")\
**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:** [January 6, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166/1 "2008-01-06T15:02:34Z")

</div>

Hi!

I’m using REST to serve content (images and SWFs) to a flash-file.  
Depending on the IP of the user, I would like to serve different  
content. To get the IP I use 'request.remote\_ip, followed by an if  
statement to return the correct content.

What I don’t know is how do I serve just the content (no XML, just  
content.jpg) as a reply from my Rails-controller? The content is not  
stored in a database, but in my public directory.

def show

```
ip = request.remote_ip

if ip=x
  return '/content/hello.jpg'
else
  return '/content/goodbye.jpg'
end

respond_to do |format|
  format.html # bbprivat.html.erb
  format.xml { render :xml => @ip }
end

```

end

Many thanks for all the help I can get!

---

<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:** [January 7, 2008, 11:04am UTC](https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166/2 "2008-01-07T11:04:28Z")

</div>

Hi Gustav,

you can use the send\_file command to return binary data to the client

e.g.  
respond\_to do |format|  
format.html { ip == x ? send\_file ‘/content/hello.jpg’, :filename =\>  
‘hello.jpg’ : send\_file ‘/content/goodbye.jpg’, :filename =\>  
‘goodbye.jpg’ }  
end

think that will work , you might want to register a new content type  
for the respond\_to block so it ‘responds\_to’ .jpg as well.

---

<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:** [January 7, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166/3 "2008-01-07T14:02:29Z")

</div>

> respond\_to do |format|  
> format.html { ip == x ? send\_file ‘/content/hello.jpg’, :filename =\>  
> ‘hello.jpg’ : send\_file ‘/content/goodbye.jpg’, :filename =\>  
> ‘goodbye.jpg’ }  
> end

I’ve tried:

def show  
ip = request.remote\_ip  
pattern = x

if pattern.match(ip)  
@file = ‘/folder/y.swf’  
else  
@file = ‘/folder/x.swf’  
end

respond\_to do |format|  
format.html { send\_file @file }  
end  
end

This returns a returns a “Cannot read file /folder/y.swf” error message.

I’ve also tried:

respond\_to do |format|  
format.html { render :file =\> @file }  
end

This returns a “No such file or directory - /folder/y.swf”, although  
“[http://localhost:3000/folder/y.swf](http://localhost:3000/folder/y.swf)” works just fine.

When substituting the @file-construct for:

format.html { send\_file ‘/folder/y.swf’, :filename =\> ‘y.swf’ }

but get the same result…

> think that will work , you might want to register a new content type  
> for the respond\_to block so it ‘responds\_to’ .jpg as well.

Where do I register new content types in the respond\_to-block? I’m a  
REST-noob, in case that wasn’t obvious 😉

Many thanks!

g.

---

<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:** [January 7, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/respond-to-rest-request-with-image-file-no-xml-etc/125166/4 "2008-01-07T14:36:13Z")

</div>

> This returns a returns a “Cannot read file /folder/y.swf” error message.

I solved it by using the full system-file path:

render :file =\> ‘/Users/gustav/Sites/sandbox/public/folder/y.swf’

Anyone know how make Rails understand to use the public-folder as root?

Also, since I get plenty of weird characters on screen, rather than the  
actual swf-file, I suppose I need to register swf to work with the  
respond\_to block.

send\_file ‘/Users/gustav/Sites/sandbox/public/folder/y.swf’

works just as good for that matter, although I get prompted to save file  
to disk.
