Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 9a053cb

Browse files
committed
Changed to fix charset=utf-8 for application/json
Fixed to be able to run both py27 and py3
1 parent 153ca05 commit 9a053cb

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

hyper/cli.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from hyper import HTTP20Connection
1818
from hyper import __version__
19-
from hyper.compat import urlencode, urlsplit
19+
from hyper.compat import is_py2, urlencode, urlsplit, write_to_stdout
2020

2121

2222
log = logging.getLogger('hyper')
@@ -161,13 +161,16 @@ def set_request_data(args):
161161
elif i.sep == SEP_QUERY:
162162
params[i.key] = i.value
163163
elif i.sep == SEP_DATA:
164-
body[i.key] = i.value
164+
value = i.value
165+
if is_py2: # pragma: no cover
166+
value = value.decode(PREFERRED_ENCODING)
167+
body[i.key] = value
165168

166169
if params:
167170
args.url.path += '?' + urlencode(params)
168171

169172
if body:
170-
content_type = 'application/json; charset=%s' % PREFERRED_ENCODING
173+
content_type = 'application/json; charset=utf-8'
171174
headers.setdefault('content-type', content_type)
172175
args.body = json.dumps(body)
173176

@@ -226,9 +229,7 @@ def main(argv=None):
226229
args = parse_argument(argv)
227230
log.debug('Commandline Argument: %s', args)
228231
data = request(args)
229-
sys.stdout.buffer.write(data.encode(PREFERRED_ENCODING, errors='replace'))
230-
sys.stdout.buffer.write(b'\n')
231-
sys.stdout.buffer.flush()
232+
write_to_stdout(data.encode(PREFERRED_ENCODING, errors='replace'))
232233

233234

234235
if __name__ == '__main__': # pragma: no cover

hyper/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def to_byte(char):
4343
def decode_hex(b):
4444
return b.decode('hex')
4545

46+
def write_to_stdout(data):
47+
sys.stdout.write(data + '\n')
48+
sys.stdout.flush()
49+
4650
# The standard zlib.compressobj() accepts only positional arguments.
4751
def zlib_compressobj(level=6, method=zlib.DEFLATED, wbits=15, memlevel=8,
4852
strategy=zlib.Z_DEFAULT_STRATEGY):
@@ -57,6 +61,10 @@ def to_byte(char):
5761
def decode_hex(b):
5862
return bytes.fromhex(b)
5963

64+
def write_to_stdout(data):
65+
sys.stdout.buffer.write(data + b'\n')
66+
sys.stdout.buffer.flush()
67+
6068
zlib_compressobj = zlib.compressobj
6169

6270
if is_py3_3:

test/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from hyper.cli import PREFERRED_ENCODING as PENC, KeyValue
6+
from hyper.cli import KeyValue
77
from hyper.cli import get_content_type_and_charset, main, parse_argument
88
from hyper.cli import set_request_data, set_url_info
99

@@ -155,7 +155,7 @@ def test_get_content_type_and_charset(response, expected):
155155
KeyValue('data2', 'test2', '=', ''),
156156
]}
157157
),
158-
{'headers': {'content-type': 'application/json; charset=%s' % PENC},
158+
{'headers': {'content-type': 'application/json; charset=utf-8'},
159159
'method': 'POST',
160160
'body': json.dumps({'data1': 'test1', 'data2': 'test2'}),
161161
}

0 commit comments

Comments
 (0)