Chalice Framework:请求未指定带有image/jpeg的Accept标头 [英] Chalice Framework: Request did not specify an Accept header with image/jpeg

查看:111
本文介绍了Chalice Framework:请求未指定带有image/jpeg的Accept标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Chalice/python应用程序返回图像.我的整个应用程序代码粘贴在下面:

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)

结果...

{代码":"BadRequest",消息":请求未指定接受 具有image/jpeg的标头,响应的内容类型为image/jpeg. 如果响应具有二进制Content-Type,则请求必须指定 匹配的Accept标头.}

为什么会这样?

我已经阅读了大量文档,并且由于最近对Chalice添加了二进制支持,因此大多数文档已经过时了:

仅出于故障排除的目的,我可以使用curl -H "accept: image/jpeg"来获取响应,但这是没有用的,因为浏览器无法以这种方式工作,并且我需要在浏览器中使用响应(HTML IMG TAG)./p>

更新

我也尝试过@app.route('/makeImage', methods=['GET'], content_types=['image/jpeg'])

结果变为{"Code":"UnsupportedMediaType","Message":"Unsupported media type: application/json"}

解决方案

我遇到了同样的问题.

如果不存在标头接受,AWS将其设置为默认application/json,我将收到base64响应.如果将header设置为images/jpeg或任何二进制内容类型,那么我得到了图像.很棒,但是网络浏览器没有设置accept标头.

但是,如果我添加

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

然后确定,我的图像API现在可以使用了. 太好了,但是现在json失败了.

目前,除了有两个API网关外,我看不到任何解决方案:一个用于json,一个用于图像.如果您真的只想要一个API网关,我认为您必须在所有json响应上使用gzip压缩将它们转换为二进制文件.

AWS API网关如何与lambda代理一起使用,而不是圣杯问题.但我同意,这是一个很大的限制

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:

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"}

解决方案

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

这篇关于Chalice Framework:请求未指定带有image/jpeg的Accept标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆