Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slumber tries to decode responses before passing them to the serializer, making it unusable for binary formats #119

Open
daisylb opened this issue Oct 16, 2015 · 2 comments

Comments

@daisylb
Copy link

daisylb commented Oct 16, 2015

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)
@specialcircumstances
Copy link

I've got a similar issue with CBOR. It never calls the dumps function in my serializer.

@specialcircumstances
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants