Sending binary data to a (Rails) RESTful endpoint via JSON / XML?

I am currently building a rail-based web application that will only service and receive data through json and xml. However, some requirements include the ability to download binary data (images).

Now, as I understand it, JSON is not intended for this ... but how do you generally solve the problem of receiving binary files / data from these two entry points into your application?

+4
source share
4 answers

I suggest encoding binary data as base64. This would make it safe to use in XML or JSON format.

http://en.wikipedia.org/wiki/Base64

+7
source

Perhaps you could take a look at the Base64 algorithm. This is used to "convert" everything to an ascii char. You can encode and decode it. It is used for web services or even for dotnet serialization.

Hope this helps a bit.

Edit: I saw a โ€œnew messageโ€, someone was faster when posting. Rails base64

+4
source

If you use Rails and json and xml, you use HTTP. "POST" is part of HTTP and is the best way to convert binary data. Base64 is a very inefficient way to do this.

If your server sends data, I would recommend putting the file path on the server in XML or JSON. Thus, your server does not need to encode base64 data, and your client, which already supports HTTP GET, can pull data without decoding. (Get / path / to / file)

To send files, your server and / or client generate a unique file name and use a two-step process; the client will send an xml or json message with fileToBeUploaded: "name of file.ext", and after sending the message there will be POST data with the specified file name. Again, the client and server will not need to encode and decode the data. This can be done with a single query using a multi-part query.

Base64 is simple, but quickly chews on the processor and / or memory depending on the size of the data and the frequency of requests. On the server side, this is also not an operation that is cached, while the operation of your web server, which reads the file from disk, is performed.

+3
source

If your images are not too large, placing them in a database with a binary RoR type makes a lot of sense. If you have database replicas, images can be copied for free to other sites, there are no problems regarding lost or widowed images, and problems with atomic transactions become much easier.

On the other hand, Nessence is right that Base64, like any level of encoding, adds load on the network, memory and processor load on transactions. If network bandwidth is your main concern, make sure your web service accepts and offers deflate / gzip compressed connections. This will reduce the cost of Base64 data at the network level, although it will cost even more memory and CPU utilization.

These are architectural issues that need to be discussed with your team and / or client.

Finally, let me tell you about RoR XML REST support. The Rails :binary database type will become <object type="binary" encoding="base64">...</object> XML objects when rendering in XML using code like this, by default:

 def show @myobject = MyObject.find(:id) respond_to do |format| format.xml { render => @myobject } end end 

This works great for GET operations, and PUT and POST operations are just as easy to write. A trap is a Rails PUT or POST operation that does not accept the same tags. This is because from_xml does not interpret the type="binary" tag, but instead searches for type="binaryBase64" . There is a patch bug on the Rails Beacon website to fix this.

+2
source

All Articles