mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix unicode errors in json.load method
HTTPConnection returns bytes in Python 2. An explicit decoding is needed in Python 3 to ensure compatibility.
This commit is contained in:
parent
cfa8bb4255
commit
66040e0c9d
1 changed files with 13 additions and 11 deletions
|
@ -27,24 +27,23 @@
|
|||
# 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
|
||||
from ansible.module_utils.six.moves import http_client
|
||||
from ansible.module_utils.six import PY2
|
||||
|
||||
# httplib/http.client connection using unix domain socket
|
||||
HTTPConnection = http_client.HTTPConnection
|
||||
HTTPSConnection = http_client.HTTPSConnection
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
|
||||
# httplib/http.client connection using unix domain socket
|
||||
import socket
|
||||
import ssl
|
||||
|
||||
try:
|
||||
from httplib import HTTPConnection, HTTPSConnection
|
||||
except ImportError:
|
||||
# Python 3
|
||||
from http.client import HTTPConnection, HTTPSConnection
|
||||
|
||||
|
||||
class UnixHTTPConnection(HTTPConnection):
|
||||
def __init__(self, path):
|
||||
|
@ -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…
Add table
Reference in a new issue