chalice icon indicating copy to clipboard operation
chalice copied to clipboard

String format problem with Python 3.6 after deployed

Open Sdcxv opened this issue 9 years ago • 2 comments

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.

Sdcxv avatar May 25 '17 08:05 Sdcxv

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_body is a str() type.
  • Update the chalice handler to ensure raw_body is a bytes() 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.

jamesls avatar May 25 '17 17:05 jamesls

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.

stealthycoin avatar Jun 05 '17 20:06 stealthycoin