Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BINARY_SUPPORT to use Content-Encoding to identify if data is binary #2170

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Update to handle error cases
Include fallbacks for cases:
- try binary, fail to text
- try text, fail to binary
monkut authored Jan 28, 2021
commit 6c66a6c49ce06ff5f410dffabbcce5d94e44258e
17 changes: 14 additions & 3 deletions zappa/handler.py
Original file line number Diff line number Diff line change
@@ -552,10 +552,21 @@ def handler(self, event, context):
content_encoding = response.headers.get("Content-Encoding", None)
binary_encodings = ("gzip", "compress", "deflate", "br")
if settings.BINARY_SUPPORT and content_encoding in binary_encodings:
zappa_returndict['body'] = base64.b64encode(response.data).decode('utf-8')
zappa_returndict["isBase64Encoded"] = True
try:
zappa_returndict["body"] = base64.b64encode(response.data).decode("utf8")
zappa_returndict["isBase64Encoded"] = True
except UnicodeDecodeError as e:
logger.exception(e)
logger.error(f"Unable to decode resulting base64 encoded response.data as 'utf8': response.data={response.data}")
logger.warning("Using response.get_data(as_text=True)")
zappa_returndict["body"] = response.get_data(as_text=True)
else:
zappa_returndict['body'] = response.get_data(as_text=True)
try:
zappa_returndict["body"] = response.get_data(as_text=True)
except UnicodeDecodeError:
# If data can't be decoded as utf-8, try processing as binary
zappa_returndict["body"] = base64.b64encode(response.data).decode("utf8")
zappa_returndict["isBase64Encoded"] = True

zappa_returndict['statusCode'] = response.status_code
if 'headers' in event: