Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
monkut committed Jan 28, 2021
2 parents 197c906 + 6c66a6c commit 32293f9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions zappa/handler.py
Expand Up @@ -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:
Expand Down

0 comments on commit 32293f9

Please sign in to comment.