# Sending/Receiving Image Data over REST

**URL:** <https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823>\
**Category:** Rails\
**Created:** [April 24, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823 "2007-04-24T17:09:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [April 24, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823/1 "2007-04-24T17:09:51Z")

</div>

Hi,

I’ve got a REST API that accepts PUT/POST requests of image data.  
(Well, that’s the plan.)

Here are the relevant lines:

My client (using ImageMagick) application reads image data from a file  
and then coverts it’s data into a blob:

image = Magick::Image::read(@custom\_image.full\_filename).first

Obj.image\_data = image.to\_blob  
Obj.save

The ‘Obj.save’ initiates a REST request (PUT/POST)that contains the  
binary image data as a member of params

My controller on the REST app can do this:

data = params[:image\_data] #=\> data is now a String

Here’s how I’m saving the image data to the file system:

File.open(path, “wb”) do |f|  
f.binmode  
f.write data  
f.close  
end

And data is saved…but the image is unreadable, (ImageMagick on the  
REST app doesn’t recognize the image as a properly formatted jpeg.

My questions are these:

1. What is the best way to send/receive image data to a REST service?
2. Is there anything terribly wrong with the approach I’ve taken?

Thanks for your help,  
EB

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [July 13, 2007, 10:58am UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823/2 "2007-07-13T10:58:45Z")

</div>

I’m curious about this too, did you find any answers to your  
question? Or do you just have to generate the image, save it  
somewhere and send the URL back in the REST API request instead of the  
blob?

On Apr 24, 5:09 pm, Elliott B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [July 13, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823/3 "2007-07-13T14:19:07Z")

</div>

Marston A. wrote:

> I’m curious about this too, did you find any answers to your  
> question? Or do you just have to generate the image, save it  
> somewhere and send the URL back in the REST API request instead of the  
> blob?
> 
> On Apr 24, 5:09 pm, Elliott B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yes, I found a solution. I Base64 encoded the image data on the sending  
side and then Base64 unencoded on the other receiving side.

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [July 13, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823/4 "2007-07-13T16:32:51Z")

</div>

Marston A. wrote:

> Elliott,
> 
> Great, simple solution. How big are the images you’re sending over?  
> Do you notice any performance penalties? I’m thinking of doing the  
> same, though the images I have are fairly small ( \< 75k).
> 
> On Jul 13, 2:19 pm, Elliott B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

The performance is acceptable so far, but I’ve been working locally, so,  
I don’t know how it behaves over the internet. -EB

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [July 13, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/sending-receiving-image-data-over-rest/92823/5 "2007-07-13T14:25:20Z")

</div>

Elliott,

Great, simple solution. How big are the images you’re sending over?  
Do you notice any performance penalties? I’m thinking of doing the  
same, though the images I have are fairly small ( \< 75k).

On Jul 13, 2:19 pm, Elliott B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
