Skip to content

Commit

Permalink
fix: allow control characters in JSON responses (#160)
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Adams <[email protected]>
  • Loading branch information
padamstx authored Mar 23, 2023
1 parent a0d3744 commit 2f09503
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ibm_cloud_sdk_core/api_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __str__(self) -> str:
def _get_error_message(response: Response) -> str:
error_message = 'Unknown error'
try:
error_json = response.json()
error_json = response.json(strict=False)
if 'errors' in error_json:
if isinstance(error_json['errors'], list):
err = error_json['errors'][0]
Expand Down
2 changes: 1 addition & 1 deletion ibm_cloud_sdk_core/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def send(self, request: requests.Request, **kwargs) -> DetailedResponse:
elif is_json_mimetype(response.headers.get('Content-Type')):
# If this is a JSON response, then try to unmarshal it.
try:
result = response.json()
result = response.json(strict=False)
except JSONDecodeError as err:
raise ApiException(
code=response.status_code,
Expand Down
5 changes: 3 additions & 2 deletions test/test_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,18 @@ def test_request_success_invalid_json():

@responses.activate
def test_request_success_response():
expected_body = '{"foo": "bar", "description": "this\nis\na\ndescription"}'
responses.add(
responses.GET,
'https://gateway.watsonplatform.net/test/api',
status=200,
body=json.dumps({'foo': 'bar'}),
body=expected_body,
content_type='application/json',
)
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
prepped = service.prepare_request('GET', url='')
detailed_response = service.send(prepped)
assert detailed_response.get_result() == {"foo": "bar"}
assert detailed_response.get_result() == {"foo": "bar", "description": "this\nis\na\ndescription"}


@responses.activate
Expand Down

0 comments on commit 2f09503

Please sign in to comment.