Returning binary data as response


#1

How can I return binary data e.g. image or zip data as a response?

-aj


#2

I haven’t really tried that, but I believe you should implement a custom renderer, which

  1. returns just Python bytes
  2. set appropriate “Content-Type” header

See: https://github.com/encode/apistar/blob/master/apistar/renderers.py#L25

So it might look smth like

class ImageRenderer(Renderer):
    media_type = 'image/png'
    charset = None

    def render(self, data: http.ResponseData) -> bytes:
        return data

Note that I haven’t run this snippet :slight_smile: