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

Add support for bytes when binary_types accept application/json #986

Closed
wants to merge 0 commits into from
Closed

Add support for bytes when binary_types accept application/json #986

wants to merge 0 commits into from

Conversation

dbritto-dev
Copy link
Contributor

@dbritto-dev dbritto-dev commented Nov 15, 2018

import json
import gzip
from chalice import Chalice, Response

app = Chalice(app_name='gzipcontent')
app.api.binary_types.append('application/json')

@app.route('/')
def index():
    blob = json.dumps({'hello': 'world'}).encode('utf-8')
    payload = gzip.compress(blob)
    return Response(body=payload, headers={'Content-Type': 'application/json', 'Content-Encoding': 'gzip'})

Fixes #988, when binary_types is application/json and you pass bytes in the body the code throws error

Description of changes:
This patch check if the body is a string and apply .encode('utf-8') if not, let me forward the bytes from gzip.compress. Without this patch the code above doesn't work.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov-io
Copy link

codecov-io commented Nov 15, 2018

Codecov Report

Merging #986 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #986   +/-   ##
=======================================
  Coverage   95.52%   95.52%           
=======================================
  Files          27       27           
  Lines        4449     4449           
  Branches      558      558           
=======================================
  Hits         4250     4250           
  Misses        128      128           
  Partials       71       71
Impacted Files Coverage Δ
chalice/app.py 96.75% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 84e6b53...9986d6a. Read the comment docs.

@joguSD
Copy link
Contributor

joguSD commented Dec 6, 2018

I've continued this PR by squashing and rebasing against the latest master in #1013

@dbritto-dev
Copy link
Contributor Author

Thank you @joguSD

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

Successfully merging this pull request may close these issues.

Chalice dont't support bytes when binary_types is application/json
3 participants