diff --git a/lib/ansible/modules/network/basics/uri.py b/lib/ansible/modules/network/basics/uri.py index cec13c82816..dfd8e14a953 100644 --- a/lib/ansible/modules/network/basics/uri.py +++ b/lib/ansible/modules/network/basics/uri.py @@ -20,19 +20,6 @@ # # see examples/playbooks/uri.yml -import cgi -import shutil -import tempfile -import datetime - -try: - import json -except ImportError: - import simplejson as json - -import ansible.module_utils.six as six - - DOCUMENTATION = ''' --- module: uri @@ -216,6 +203,23 @@ EXAMPLES = ''' ''' +import cgi +import datetime +import os +import shutil +import tempfile + +try: + import json +except ImportError: + import simplejson as json + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.pycompat24 import get_exception +import ansible.module_utils.six as six +from ansible.module_utils._text import to_text +from ansible.module_utils.urls import fetch_url, url_argument_spec + def write_file(module, url, dest, content): # create a tempfile with some test content @@ -445,11 +449,17 @@ def main(): # Default content_encoding to try content_encoding = 'utf-8' - if 'content_type' in uresp: - content_type, params = cgi.parse_header(uresp['content_type']) + content_type_key = None + for key in uresp: + # Py2: content_type; Py3: Content_type + if 'content_type' == key.lower(): + content_type_key = key + break + if content_type_key is not None: + content_type, params = cgi.parse_header(uresp[content_type_key]) if 'charset' in params: content_encoding = params['charset'] - u_content = unicode(content, content_encoding, errors='replace') + u_content = to_text(content, encoding=content_encoding) if 'application/json' in content_type or 'text/json' in content_type: try: js = json.loads(u_content) @@ -457,7 +467,7 @@ def main(): except: pass else: - u_content = unicode(content, content_encoding, errors='replace') + u_content = to_text(content, encoding=content_encoding) if resp['status'] not in status_code: uresp['msg'] = 'Status code was not %s: %s' % (status_code, uresp.get('msg', '')) @@ -468,9 +478,5 @@ def main(): module.exit_json(changed=changed, **uresp) -# import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.urls import * - if __name__ == '__main__': main()