Fix unicode errors in json.load method

HTTPConnection returns bytes in Python 2. An explicit
decoding is needed in Python 3 to ensure compatibility.
pull/21111/merge
Esteban Echeverry 8 years ago committed by Toshio Kuratomi
parent cfa8bb4255
commit 66040e0c9d

@ -27,23 +27,22 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import socket
import ssl
from ansible.module_utils.urls import generic_urlparse
from ansible.module_utils.six.moves.urllib.parse import urlparse
try:
import json
except ImportError:
import simplejson as json
from ansible.module_utils.six.moves import http_client
from ansible.module_utils.six import PY2
# httplib/http.client connection using unix domain socket
import socket
import ssl
HTTPConnection = http_client.HTTPConnection
HTTPSConnection = http_client.HTTPSConnection
try:
from httplib import HTTPConnection, HTTPSConnection
import json
except ImportError:
# Python 3
from http.client import HTTPConnection, HTTPSConnection
import simplejson as json
class UnixHTTPConnection(HTTPConnection):
@ -110,7 +109,10 @@ class LXDClient(object):
body = json.dumps(body_json)
self.connection.request(method, url, body=body)
resp = self.connection.getresponse()
resp_json = json.loads(resp.read())
resp_data = resp.read()
if not PY2:
resp_data = resp_data.decode('utf-8')
resp_json = json.loads(resp_data)
self.logs.append({
'type': 'sent request',
'request': {'method': method, 'url': url, 'json': body_json, 'timeout': timeout},

Loading…
Cancel
Save