Waynn Lue
Waynn Lue

Reputation: 11385

How do I send both ASCII and binary data in one HTTP response?

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.

Upvotes: 0

Views: 1275

Answers (2)

irreputable
irreputable

Reputation: 45463

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.

Upvotes: 0

Perception
Perception

Reputation: 80633

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.

Upvotes: 3

Related Questions