apig-wsgi
apig-wsgi copied to clipboard
Errors if Django gzip middleware used
Python Version
3.11
Package Version
2.18.0
Description
[ERROR] UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
Traceback (most recent call last):
File "/var/task/app/handlers.py", line 102, in app
return _real_handler(event, context)
File "/var/task/apig_wsgi/__init__.py", line 99, in handler
return response.as_apig_response()
File "/var/task/apig_wsgi/__init__.py", line 329, in as_apig_response
response["body"] = self.body.getvalue().decode("utf-8")
Called like:
from apig_wsgi import make_lambda_handler
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
_real_handler = make_lambda_handler(application)
with "django.middleware.gzip.GZipMiddleware", in middleware.
Ah do we perhaps just need binary_support=False? This is with ALB, so I gather the implicit None there is enabling it.
I believe this was fixed in #80 , back in version 2.3.0 (2019-08-19).
~You’re not using version 2.18.0 as you report because the source in your traceback does not match. Please check which version you are actually using.~
Ah, no, sorry the source does match.
I think binary_support=True may fix it, but ideally we would send binary automatically. This method should be returning True, but isn’t:
https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/src/apig_wsgi/init.py#L276-L289
That could be a problem with my headers rather than that detection though I suppose.
I will try it shortly, thanks for getting back to me so quickly.
It's because of the first
if not self.binary_support:
return False
isn't it? I'm not setting it, and so: https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/src/apig_wsgi/init.py#L74-L76
(which seems contrary to the readme, hence I got it the wrong way around in my first reply: https://github.com/adamchainz/apig-wsgi/blob/dc220949c4bf9b0aeb0eb49fdd4767af66fdb776/README.rst?plain=1#L81?)
I think
binary_support=Truemay fix it
and I can confirm it does indeed! Thanks again.
As above though I think the intention was for version == "alb" to do binary_support != False rather than or False, i.e. to default it True when None, so it wouldn't have been necessary for me to set it explicitly.
Okay, I just came back to this. I think I fixed it in #528, releasing shortly. Would you be able to check?
Yep seems fine now without the binary_support=True arg too, thanks!