0

I'm currently using Java 1.4.2 on the client side to talk to a Rails 3 server. When we originally designed the communication, all the information returned was ASCII (alphanumeric), so we would use JSON and return it as a map. Now we find the need to return binary data (an image) as well, but we don't want to make two HTTP requests.

What's the best way to send the existing ASCII information and the binary data in one HTTP response? My current thought is to use base64 or hex-encoding, but I don't know if that's the best way. An easy way to encode on the server (Ruby) side and decode on the client (Java 1.4.2) is also a huge plus.

1
  • I would go with Base64. It's simple, well known and supported. Commented Jan 10, 2012 at 1:34

2 Answers 2

3

The HTTP protocol allows for returning responses as multipart/mixed. When doing so, the response is demarcated into zero or more parts, with each part separated by a boundary. Each of the parts (can) have its own headers describing the content of the part (Content-Type, Content-Length, Content-Disposition, etc). So you could have one part in the response be ASCII text and another be an octet stream.

Using this response type is probably your best bet for sending back mixed content in one HTTP call, but will require you make some modifications in your Rails code.

Special note, if you have browser based clients using the same server services, not all browsers handle multipart/mixed responses correctly.

Sign up to request clarification or add additional context in comments.

4 Comments

So that's the most performant way. Base 64 sounds better regarding compatibility and development requirements, configuration etc. I guess it's up to the user to decide which one is better.
Not sure what you're referring too. Base 64 is an encoding scheme, and doesn't provide any protocols for content definition or description.
Absolutely true, you would need something on top of HTTP for that. XML would come to mind for some reason or another. The Base 64 encoding would be running in it. Not necessarily super-efficient, but I can think of a few proven technologies that use that kind of messaging.
Yeah, I'm actually tempted to go with Base64 as the simplest way to add existing support to our code. It's well supported in both Ruby and Java, and requires the smallest change to what we currently have. (Can I accept the comment of @Sergio Tulentsev as an answer? This is my first SO question. :))
0

HTTP body is binary. There is no need to ASCII-ize any data. Base64 is not relevant here.

If you control both client and server, the easiest solution would be to have your own format. Do whatever you are comfortable with to pack/unpack the data.

1 Comment

Yeah, I think the main problem is that we already have a method for JSON-encoding/decoding our data (since it was ASCII to begin with), so we were hoping not to have to re-write that bit to have our own format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.