Skip to content Skip to sidebar Skip to footer

Chalice Framework: Request Did Not Specify An Accept Header With Image/jpeg

I want to return an image from a Chalice/python application. My entire application code is pasted below: from chalice import Chalice, Response import base64 app = Chalice(app_name

Solution 1:

I had the same issue.

If no header accept is present, AWS set it to default application/json and I receive a base64 response. If I set accept to images/jpeg or any binary content type in header, then I got the images. Great but web browser to not set the accept header.

But if I add

app.api.binary_types =['*/*']

then ok my images apis now works. Great but now the json ones fail.

Currently I do not see any solution except having two API gateway : one for json and one for images. If you really want only one API Gateway, I think you have to use gzip conpression on all you json response to convert them to binaries.

It is more how AWS API Gateway works with lambda proxy than a Chalice issue. But I agree, it is a big limitation


Solution 2:

There was a bug in Chalice that was fixed on 14-May-2019 and documented here:

https://github.com/aws/chalice/issues/1095

In addition to installing the latest Chalice directly from GitHub, I also had to add:

app.api.binary_types =['*/*']

in app.py.

The final working code looks like this:

from chalice import Chalice, Response
import base64

app = Chalice(app_name='hello')
app.api.binary_types =['*/*']

@app.route('/makeImage', methods=['GET'])
def makeImage():
    return Response(
        base64.b64decode(
            "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
        ),
        headers={
            'Content-Type': 'image/jpeg'
        },
        status_code=200)

Post a Comment for "Chalice Framework: Request Did Not Specify An Accept Header With Image/jpeg"