Slumber tries to decode responses before passing them to the serializer, making it unusable for binary formats
I've been trying to use Slumber with an API that exposes both JSON and MessagePack; it works fine with the former but doesn't work at all when I try to write a custom serialiser for the latter.
It looks like Slumber is trying to decode the response as text in slumber.Resource._try_to_serialize_response; because MessagePack is a binary format, requests.utils.guess_json_utf is returning None, and resp.content.decode(None) works exactly as well as one would expect, so the serialiser doesn't even get called and the consumer code gets returned a raw bytestring.
I'm using Slumber 0.7.1 on Python 3.4.3. The code for my MessagePack serialiser is as follows:
import msgpack
from slumber.serialize import BaseSerializer
class MessagePackSerializer(BaseSerializer):
key = "msgpack"
content_types = ("application/msgpack",)
def loads(self, data):
return msgpack.loads(data)
def dumps(self, data):
return msgpack.dumps(data)
I've got a similar issue with CBOR. It never calls the dumps function in my serializer.
This was my fix in slumber.Resource._try_to_serialize_response
if stype.key == 'cbor':
return stype.loads(resp.content)
if type(resp.content) == bytes:
swap cbor for msgpack and it should work I guess