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

Python 3 jsonify on b64encode(asset_report_pdf) throws TypeError exception #53

Closed
gregbehm opened this issue Apr 24, 2019 · 2 comments
Closed

Comments

@gregbehm
Copy link
Contributor

The server.py Asset Report code attempts to jsonify the base-64-encoded PDF report:

return jsonify({
    'error': None,
    'json': asset_report_json,
    'pdf': base64.b64encode(asset_report_pdf),
  })

For Python 3, this throws a TypeError exception:

[2019-04-24 15:10:09,368] ERROR in app: Exception on /assets [GET]
Traceback (most recent call last):
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "server.py", line 158, in get_assets
    'pdf': base64.b64encode(asset_report_pdf),
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/json/__init__.py", line 321, in jsonify
    dumps(data, indent=indent, separators=separators) + '\n',
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/json/__init__.py", line 179, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/site-packages/flask/json/__init__.py", line 81, in default
    return _json.JSONEncoder.default(self, o)
  File "/Users/gbehm/miniconda3/envs/plaid37/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

In Python 3, base64.b64encode(asset_report_pdf) accepts a bytes-like object and returns encoded bytes. The interface changed from Python 2.7, where base64.b64encode(asset_report_pdf) accepts a str and returns an encoded str.

For Python 3, the output of base64.b64encode(asset_report_pdf) should be converted to str before attempting to jsonify.

@gregbehm
Copy link
Contributor Author

I'm planning to open a PR for a fix.

@davidzhanghp
Copy link
Contributor

thank you for opening the pr @gregbehm

SpencerHHurst pushed a commit to SpencerHHurst/quickstart that referenced this issue Jul 10, 2024
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