Chalice Framework: Request did not specify an Accept header with image/jpeg

喜欢而已 提交于 2019-12-11 06:15:34

问题


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='hello')

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

The result...

{"Code":"BadRequest","Message":"Request did not specify an Accept header with image/jpeg, The response has a Content-Type of image/jpeg. If a response has a binary Content-Type then the request must specify an Accept header that matches."}

Why does this happen?

I have poured through a ton of documentation already and most of it is outdated as binary support was added to Chalice very recently:

  • https://github.com/aws/chalice/pull/352
  • https://github.com/aws/chalice/issues/592
  • https://github.com/aws/chalice/issues/348
  • AWS Chalice Return an Image File from S3 (Warning: the sole answer to this question is COMPLETELY WRONG)
  • https://chalice.readthedocs.io/en/latest/api.html
  • https://github.com/aws/chalice/issues/391 (issue WRONGLY CLOSED in 2017 without a resolution)
  • https://github.com/aws/chalice/issues/1095 is a re-open of 391 above

Just for troubleshooting purposes I'm able to obtain a response by using curl -H "accept: image/jpeg", but this is useless since browsers to not work this way, and I need to use the response in a browser (HTML IMG TAG).

UPDATE

I also tried @app.route('/makeImage', methods=['GET'], content_types=['image/jpeg'])

And result became {"Code":"UnsupportedMediaType","Message":"Unsupported media type: application/json"}


回答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




回答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)


来源:https://stackoverflow.com/questions/56159447/chalice-framework-request-did-not-specify-an-accept-header-with-image-jpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!