joshmarshall / jsonrpclib

A Python JSON-RPC over HTTP that mirrors xmlrpclib syntax.
Other
447 stars 144 forks source link

binary data in return? #37

Open stuartaw opened 9 years ago

stuartaw commented 9 years ago

We often return binary (actually complete dynamic png/jpeg images), for example: filestring=self.GetFrame(format,size) return xmlrpclib.Binary(filestring) where filestring is the binary data of a full jpg or png.

however when I glue this into jsonrpclib instead of xmlrpclib, it receive end chokes (as it does with the raw binary data). ProtocolError: (-32603, u"<type 'exceptions.UnicodeDecodeError'>:'utf8' codec can't decode byte 0x89 in position 0: invalid start byte")

There must be some way of getting such data through? from what I see the above base64 encode path should be possible? it would of course be very nice if the dest end auto-decoded it as it possible in xmlrpc. any thoughts?

ghost commented 9 years ago

Running up against a similar issue, except there's a client on the other end that expects hex-encoded data and it turned out I was accidentally passing a byte array. So now I just encode the data as hex before I send it. In your case, you'd probably want to encode it as base64.

I'm not sure jsonrpclib should coerce values to and from byte data automatically. What is sent should be what is received, and it makes sense for it to fail if you ask it to send something that has no native representation in JSON. Explicit is better than implicit, right?

stuartaw commented 7 years ago

Sorry, I know its been a while. There are two problems with this as it breaks using JSONRPC to publish existing APIs. 1) In my case, I have an API that can be used via local call, XMLRPC, and hopefully one day JSONRPC. One of its functions returns jpeg or png data. for that to work with JSONRPC the API must be different for this RPC method. not nice. 2) How are we supposed to deal with unusual characters that are not unicode encoded? it would be nice to live in a world where everyone uses unicode.. but we do not.

I cannot think of another RPC standard that cannot copy with binary data.. The whole idea of RPC, unfortunately, is to emulate the functionality of local function calls - can you imagine having to encode/decode all binary data in your local function calls?