0

I'm trying to serve base64 encoded image files and failing. Either I get UTF-8 encoded responses or the line return response errors in an interesting way. Mostly everything I've tried can be seen as commented out code in the excerpt below. Details of the traceback follow that.

My question is: How can I return base64 encoded files?

        #import base64
        #with open(sPath, "rb") as image_file:
            #encoded_string = base64.b64encode(image_file.read())
        dContentTypes = {
        'bmp'   : 'image/bmp',
        'cod'   : 'image/cis-cod',
        'git'   : 'image/gif',
        'ief'   : 'image/ief',
        'jpe'   : 'image/jpeg',
        .....
        }
        sContentType = dContentTypes[sExt]
        response = FileResponse(
                            sPath,
                            request=request,
                            content_type= sContentType#+';base64',
                            #content_encoding = 'base_64'
                            #content_encoding = encoded_string
                            )
        return response

Uncommenting the line #content_encoding = encoded_string gives me the error:

 AssertionError: Header value b'/9j/4AAQSkZJRgABAQAA' is not a string in ('Content-Encoding', b'/9j/4AAQSkZJRgABAQAA....')
Sheena
  • 15,590
  • 14
  • 75
  • 113
  • Why not serve the images *directly*? Base64 encoded resources are only ever needed when embedded *directly* in CSS or HTML. When serving an HTTP response, you just serve the resource, unencoded. – Martijn Pieters Sep 16 '13 at 16:54
  • @MartijnPieters: it is the nature of the beast... I have one script that grabs images and stores them somewhere nice and I have a page that needs to display them. Unfortunately I can't serve them in the normal way because of configuration I don't have access to. Also due to the nature of the application I need to be able to fetch some of them dynamically (I could just hide them somewhere on the page and unhide them as needed but there are quite a lot of them) – Sheena Sep 24 '13 at 17:46

2 Answers2

1

The error you are seeing is telling you that Content-Type is not a string. Content-Type is an HTTP header. And as far as I know, HTTP headers must be strings.

I believe the base64 encoded file you want to pass as the body of the response. FileResponse is not appropriate here since you presumably want to pass encoded string as the body and FileResponse expects a path that it then reads in and sets the body.

Tom Willis
  • 5,250
  • 23
  • 34
1

FileResponse is used specifically for uploading a file as a response (hence the path argument). In you're case you want to base64-encode the file before uploading it. This means no FileResponse.

Since you've read the file into memory you can just upload the content in a Response.

response = Response(encoded_string,
                    request=request,
                    content_type=sContentType+';base64')

I'm not actually sure how content_encoding compares to the ;base64 on the type, but I think the encoding is used more commonly for gzipped content. YMMV.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70