From 91fd98a2bdf2ade0c77d602dd60c0de197a7372c Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Thu, 31 May 2018 13:20:17 -0400 Subject: [PATCH] Complain if API response is not JSON (#40966) --- lib/ansible/plugins/httpapi/eos.py | 6 +++++- lib/ansible/plugins/httpapi/nxos.py | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/httpapi/eos.py b/lib/ansible/plugins/httpapi/eos.py index b079351d8f..dc5d2059b6 100644 --- a/lib/ansible/plugins/httpapi/eos.py +++ b/lib/ansible/plugins/httpapi/eos.py @@ -34,7 +34,11 @@ class HttpApi: headers = {'Content-Type': 'application/json-rpc'} response = self.connection.send('/command-api', request, headers=headers, method='POST') - response = json.loads(to_text(response.read())) + response_text = to_text(response.read()) + try: + response = json.loads(response_text) + except ValueError: + raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text)) results = handle_response(response) if self._become: diff --git a/lib/ansible/plugins/httpapi/nxos.py b/lib/ansible/plugins/httpapi/nxos.py index d6cfc4fe1d..13385da58f 100644 --- a/lib/ansible/plugins/httpapi/nxos.py +++ b/lib/ansible/plugins/httpapi/nxos.py @@ -29,7 +29,12 @@ class HttpApi: headers = {'Content-Type': 'application/json'} response = self.connection.send('/ins', request, headers=headers, method='POST') - response = json.loads(to_text(response.read())) + response_text = to_text(response.read()) + try: + response = json.loads(response_text) + except ValueError: + raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text)) + results = handle_response(response) if self._become: