Skip to content

Commit ee27269

Browse files
committed
fix: encode serialized JSON string as UTF-8
1 parent 5395f65 commit ee27269

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import json as json_import
1919
import logging
2020
import platform
21-
import sys
2221
from http.cookiejar import CookieJar
2322
from os.path import basename
2423
from typing import Dict, List, Optional, Tuple, Union
@@ -373,14 +372,13 @@ def prepare_request(self,
373372
params = cleanup_values(params)
374373
request['params'] = params
375374

376-
if sys.version_info >= (3, 0) and isinstance(data, str):
375+
if isinstance(data, str):
377376
data = data.encode('utf-8')
378-
379-
if data and isinstance(data, dict):
377+
elif isinstance(data, dict) and data:
380378
data = remove_null_values(data)
381379
if headers.get('content-type') is None:
382380
headers.update({'content-type': 'application/json'})
383-
data = json_import.dumps(data)
381+
data = json_import.dumps(data).encode('utf-8')
384382
request['data'] = data
385383

386384
self.authenticator.authenticate(request)

test/test_base_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ def test_files_duplicate_parts():
786786
def test_json():
787787
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
788788
req = service.prepare_request('POST', url='', headers={
789-
'X-opt-out': True}, data={'hello': 'world'})
790-
assert req.get('data') == "{\"hello\": \"world\"}"
789+
'X-opt-out': True}, data={'hello': 'world', 'fóó': 'bår'})
790+
assert req.get('data') == b'{"hello": "world", "f\\u00f3\\u00f3": "b\\u00e5r"}'
791791

792792

793793
def test_trailing_slash():

0 commit comments

Comments
 (0)