String format problem with Python 3.6 after deployed
I had upgraded to Python 3.6 with chalice 0.8.2 But the string format between chalice local mode and deployed to AWS is different.
In local, I got the JSON request body using Python3 format.
request_body = urllib.parse.parse_qs(app.current_request.raw_body)
and print it out.
Serving on localhost:8000
{b'brand': [b'svs'], b'model': [b'pb'], b'version': [b'1.1']}
But after deployed it. I got this in cloudwatch,
{'brand': ['svs'], 'model': ['pb16'], 'version': ['1.1']}
Did the request body will be replaced by str type in API Gateway? The local mode should have the same result of the output with the deployed one.
I can confirm this is an issue, local mode and API gateway have different types. Using this:
import sys
from chalice import Chalice
if sys.version_info[0] == 3:
# Python 3 imports.
from urllib.parse import urlparse, parse_qs
else:
# Python 2 imports.
from urlparse import urlparse, parse_qs
app = Chalice(app_name='helloworld')
@app.route('/', methods=['POST'],
content_types=['application/x-www-form-urlencoded'])
def index():
body = parse_qs(app.current_request.raw_body)
print(repr(app.current_request.raw_body))
print(repr(body))
return body
You'll get bytes() in local mode on python3 and str() when you deploy to apigateway. So we have two options:
- Update local to ensure
raw_bodyis astr()type. - Update the chalice handler to ensure
raw_bodyis abytes()type.
I think the latter is the better option, especially once the binary content type support lands. I'm assuming raw_body is a bytes() type in that PR, which means you have an inconsistent type for .raw_body depending on the content type. We'd need to evaluate the impact of changing this, but I would expect something like raw_body to a bytes() type.
So this is no longer an issue due to #352 since Chalice now ensures that the raw_body is a bytes() type.
That means that the code:
import sys
from chalice import Chalice
if sys.version_info[0] == 3:
# Python 3 imports.
from urllib.parse import urlparse, parse_qs
else:
# Python 2 imports.
from urlparse import urlparse, parse_qs
app = Chalice(app_name='helloworld')
@app.route('/', methods=['POST'],
content_types=['application/x-www-form-urlencoded'])
def index():
body = parse_qs(app.current_request.raw_body)
print(repr(app.current_request.raw_body))
print(repr(body))
return body
Would need to be re-written like so (at least under Python3):
import sys
from chalice import Chalice
if sys.version_info[0] == 3:
# Python 3 imports.
from urllib.parse import urlparse, parse_qs
else:
# Python 2 imports.
from urlparse import urlparse, parse_qs
app = Chalice(app_name='helloworld')
@app.route('/', methods=['POST'],
content_types=['application/x-www-form-urlencoded'])
def index():
body = parse_qs(app.current_request.raw_body.decode('utf-8'))
print(repr(app.current_request.raw_body))
print(repr(body))
return body
Since otherwise it will return a dict with byte keys and values of lists of bytes which the JSON response parser will error out on.