我有一个最小完整的,可验证的示例圣杯应用程序:
from chalice import Chalice, Response
from urllib.request import Request, urlopen
from urllib.parse import unquote
import io
app = Chalice(app_name='photo')
app.api.binary_types =['*/*']
@app.route('/get-photo/{url}/{maxWidth}/{maxHeight}', methods=['GET'])
def getPhoto(url, maxWidth, maxHeight):
return Response(
load(unquote(url), int(maxWidth), int(maxHeight)),
headers={ 'Content-Type': 'image/jpeg' },
status_code=200)
def load(url, maxWidth, maxHeight):
print(url)
req = Request(url)
req.add_header('accept', 'image/jpeg')
image = urlopen(req).read()
return thumbnail(image, maxWidth, maxHeight)
from PIL import Image
def thumbnail(image, maxWidth, maxHeight):
im = Image.open(io.BytesIO(image))
im.thumbnail((maxWidth,maxHeight), Image.ANTIALIAS)
with io.BytesIO() as output:
im.save(output, format="JPEG")
return output.getvalue()
@app.route('/echo', methods=['POST'])
def preload():
# MCVE
return Response(app.current_request.json_body, status_code=200)
从目前的情况来看,/get-photo
路由工作正常,但是/echo
端点在用curl -H "Content-Type: application/json" -X POST -d '{"test":1}' https://...
调用时失败
[ERROR] ValueError: Expected bytes type for body with binary Content-Type. Got <class 'str'> type body instead.
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 836, in __call__
response = response.to_dict(self.api.binary_types)
File "/var/task/chalice/app.py", line 375, in to_dict
self._b64encode_body_if_needed(response, binary_types)
File "/var/task/chalice/app.py", line 392, in _b64encode_body_if_needed
body = self._base64encode(body)
File "/var/task/chalice/app.py", line 400, in _base64encode
% type(data))
如果我删除app.api.binary_types =['*/*']
,那么/echo
工作正常,但是我遇到了Chalice Framework: Request did not specify an Accept header with image/jpeg中描述的问题
如何使这两个路由在同一个应用程序中工作?你知道吗
通过将最后一行改为
更新
这项工作了一段时间,直到我遇到了额外的问题。然后,我根据返回类型将应用程序分为两部分。你知道吗
相关问题 更多 >
编程相关推荐