|
6 | 6 | Command line interface for Hyper inspired by Httpie. |
7 | 7 | """ |
8 | 8 | import json |
| 9 | +import locale |
9 | 10 | import logging |
10 | 11 | import sys |
11 | 12 | from argparse import ArgumentParser, RawTextHelpFormatter |
|
15 | 16 |
|
16 | 17 | from hyper import HTTP20Connection |
17 | 18 | from hyper import __version__ |
18 | | -from hyper.compat import urlencode, urlsplit |
| 19 | +from hyper.compat import is_py2, urlencode, urlsplit, write_to_stdout |
19 | 20 |
|
20 | 21 |
|
21 | 22 | log = logging.getLogger('hyper') |
22 | 23 |
|
23 | | -FILESYSTEM_ENCODING = sys.getfilesystemencoding() |
| 24 | +PREFERRED_ENCODING = locale.getpreferredencoding() |
24 | 25 |
|
25 | 26 | # Various separators used in args |
26 | 27 | SEP_HEADERS = ':' |
@@ -160,13 +161,16 @@ def set_request_data(args): |
160 | 161 | elif i.sep == SEP_QUERY: |
161 | 162 | params[i.key] = i.value |
162 | 163 | elif i.sep == SEP_DATA: |
163 | | - 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 |
164 | 168 |
|
165 | 169 | if params: |
166 | 170 | args.url.path += '?' + urlencode(params) |
167 | 171 |
|
168 | 172 | if body: |
169 | | - content_type = 'application/json; charset=%s' % FILESYSTEM_ENCODING |
| 173 | + content_type = 'application/json' |
170 | 174 | headers.setdefault('content-type', content_type) |
171 | 175 | args.body = json.dumps(body) |
172 | 176 |
|
@@ -224,7 +228,8 @@ def request(args): |
224 | 228 | def main(argv=None): |
225 | 229 | args = parse_argument(argv) |
226 | 230 | log.debug('Commandline Argument: %s', args) |
227 | | - print(request(args)) |
| 231 | + data = request(args) |
| 232 | + write_to_stdout(data.encode(PREFERRED_ENCODING, errors='replace')) |
228 | 233 |
|
229 | 234 |
|
230 | 235 | if __name__ == '__main__': # pragma: no cover |
|
0 commit comments