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

Binary Support Not Working for Flask Application #1263

Closed
valeriozhang opened this issue Aug 14, 2023 · 5 comments
Closed

Binary Support Not Working for Flask Application #1263

valeriozhang opened this issue Aug 14, 2023 · 5 comments

Comments

@valeriozhang
Copy link

valeriozhang commented Aug 14, 2023

Context

I am trying to serve byte responses from my zappa flask app. I enabled binary content types on my API gateway but i am still getting the error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
when i run zappa tail

Expected Behavior

I should be able to receive byte responses from my flask API Gateway

Actual Behavior

receiving UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte error

Possible Fix

(https://docs.aws.amazon.com/apigateway/latest/developerguide/lambda-proxy-binary-media.html)

Steps to Reproduce

@app.route('/')
def get_bytes_obj(path, format):
try:
return requests.get("https://www.google.com").content
except Exception as e:
print(e)

Your Environment

  • Zappa version used: 0.57.0
  • Operating System and Python version:
  • The output of pip freeze:
    Flask==2.2.2
    boto3==1.26.43
    requests==2.28.1
    PyJWT==2.6.0
    cryptography==39.0.1
    zappa==0.57.0
  • Link to your project (optional):
  • Your zappa_settings.json:
    {
    "prod": {
    "app_function": "app.app",
    "aws_region": "us-east-1",
    "profile_name": "default",
    "project_name": "flask-image-ser",
    "runtime": "python3.9",
    "s3_bucket": "flask-image-server-v2",
    "binary_support": true,
    "payload_compression": true
    }
    }
@monkut
Copy link
Collaborator

monkut commented Aug 16, 2023

This may be a flask issue. By default I believe that flask attempts to return text/json as the response.

If you want to respond to your request with binary data, you need to prevent flask from attempting to convert the response content to text. I believe send_file can be used to return a binary response.

You can determine if this is a zappa issue by running flask locally and confirming that the error does not occur.

@valeriozhang
Copy link
Author

Error does not occur locally, only when deployed to lambdas and api gateway.

@monkut
Copy link
Collaborator

monkut commented Aug 16, 2023

Can you provide the traceback with line numbers?

@valeriozhang
Copy link
Author

I did this instead and this worked!! just needed to wrap the response object

@app.route('/test.png') def get_test_png(): r = requests.get("https://www.google.com").content return Response( r.content, status=r.status_code, content_type=r.headers['content-type'], )

@monkut
Copy link
Collaborator

monkut commented Aug 17, 2023

Closing this as it appears to be resolved by adjusting the response in the flask app.

@monkut monkut closed this as completed Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants