Perform Response body encoding/decoding Related: https://github.com/zappa/Zappa/issues/908 API Gateway requires binary data be base64 encoded: https://aws.amazon.com/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/ When BINARY_SUPPORT i
(response: Response, settings: ModuleType)
| 343 | |
| 344 | @staticmethod |
| 345 | def _process_response_body(response: Response, settings: ModuleType) -> Tuple[str, bool]: |
| 346 | """ |
| 347 | Perform Response body encoding/decoding |
| 348 | |
| 349 | Related: https://github.com/zappa/Zappa/issues/908 |
| 350 | API Gateway requires binary data be base64 encoded: |
| 351 | https://aws.amazon.com/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/ |
| 352 | When BINARY_SUPPORT is enabled the body is base64 encoded in the following cases: |
| 353 | |
| 354 | - Content-Encoding defined, commonly used to specify compression (br/gzip/deflate/etc) |
| 355 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding |
| 356 | Content like this must be transmitted as b64. |
| 357 | |
| 358 | - Response assumed binary when Response.mimetype does |
| 359 | not start with an entry defined in 'handle_as_text_mimetypes' |
| 360 | """ |
| 361 | encode_body_as_base64 = False |
| 362 | if settings.BINARY_SUPPORT: |
| 363 | handle_as_text_mimetypes = DEFAULT_TEXT_MIMETYPES |
| 364 | additional_text_mimetypes = getattr(settings, "ADDITIONAL_TEXT_MIMETYPES", None) |
| 365 | if additional_text_mimetypes: |
| 366 | handle_as_text_mimetypes += tuple(additional_text_mimetypes) |
| 367 | |
| 368 | if response.headers.get("Content-Encoding"): # Assume br/gzip/deflate/etc encoding |
| 369 | encode_body_as_base64 = True |
| 370 | |
| 371 | # werkzeug Response.mimetype: lowercase without parameters |
| 372 | # https://werkzeug.palletsprojects.com/en/2.2.x/wrappers/#werkzeug.wrappers.Request.mimetype |
| 373 | elif not response.mimetype.startswith(handle_as_text_mimetypes): |
| 374 | encode_body_as_base64 = True |
| 375 | |
| 376 | if encode_body_as_base64: |
| 377 | body = base64.b64encode(response.data).decode("utf8") |
| 378 | else: |
| 379 | # response.data decoded by werkzeug |
| 380 | # https://werkzeug.palletsprojects.com/en/2.2.x/wrappers/#werkzeug.wrappers.Request.get_data |
| 381 | body = response.get_data(as_text=True) |
| 382 | |
| 383 | return body, encode_body_as_base64 |
| 384 | |
| 385 | @staticmethod |
| 386 | def _is_asgi_app(app: Any) -> bool: |