# Buggy code
def _query_http(self, dl_path, repo_info):
    """
    Download files via http
    """
    query = None
    response = None

    try:
        if "username" in repo_info:
            try:
                if "password" in repo_info:
                    query = http.query(
                        dl_path,
                        text=True,
                        username=repo_info["username"],
                        password=repo_info["password"],
                    )
                else:
                    raise SPMException(
                        "Auth defined, but password is not set for username: '{0}'".format(
                            repo_info["username"]
                        )
                    )
            except SPMException as exc:
                self.ui.error(six.text_type(exc))
        else:
            query = http.query(dl_path, text=True)
    except SPMException as exc:
        self.ui.error(six.text_type(exc))

    try:
        if query:
            if "SPM-METADATA" in dl_path:
                response = salt.utils.yaml.safe_load(query.get("text", "{}"))
            else:
                response = query.get("text")
        else:
            raise SPMException("Response is empty, please check for Errors above.")
    except SPMException as exc:
        self.ui.error(six.text_type(exc))

    return response


# Traceback (UnicodeDecodeError)
$ spm install users
Installing packages:
users

Proceed? [N/y] y
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 12: invalid continuation byte
Traceback (most recent call last):
File "/usr/bin/spm", line 12, in <module>
salt_spm()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 527, in salt_spm
spm.run()
File "/usr/lib/python3/dist-packages/salt/cli/spm.py", line 41, in run
client.run(self.args)
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 125, in run
self._install(args)
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 373, in _install
outf.write(self._query_http(dl_url, repo_info['info']))
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 668, in _query_http
query = http.query(dl_path, text=True, backend='requests')
File "/usr/lib/python3/dist-packages/salt/utils/http.py", line 385, in query
body = body.decode(result.encoding or 'utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 12: invalid continuation byte
Traceback (most recent call last):
File "/usr/bin/spm", line 12, in <module>
salt_spm()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 527, in salt_spm
spm.run()
File "/usr/lib/python3/dist-packages/salt/cli/spm.py", line 41, in run
client.run(self.args)
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 125, in run
self._install(args)
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 373, in _install
outf.write(self._query_http(dl_url, repo_info['info']))
File "/usr/lib/python3/dist-packages/salt/spm/__init__.py", line 668, in _query_http
query = http.query(dl_path, text=True, backend='requests')
File "/usr/lib/python3/dist-packages/salt/utils/http.py", line 385, in query
body = body.decode(result.encoding or 'utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 12: invalid continuation byte

# Fixed code
def _query_http(self, dl_path, repo_info, decode_body=True):
    """
    Download files via http
    """
    query = None
    response = None

    try:
        if "username" in repo_info:
            try:
                if "password" in repo_info:
                    query = http.query(
                        dl_path,
                        text=True,
                        username=repo_info["username"],
                        password=repo_info["password"],
                        decode_body=decode_body,
                    )
                else:
                    raise SPMException(
                        "Auth defined, but password is not set for username: '{0}'".format(
                            repo_info["username"]
                        )
                    )
            except SPMException as exc:
                self.ui.error(six.text_type(exc))
        else:
            query = http.query(dl_path, text=True, decode_body=decode_body)
    except SPMException as exc:
        self.ui.error(six.text_type(exc))

    try:
        if query:
            if "SPM-METADATA" in dl_path:
                response = salt.utils.yaml.safe_load(query.get("text", "{}"))
            else:
                response = query.get("text")
        else:
            raise SPMException("Response is empty, please check for Errors above.")
    except SPMException as exc:
        self.ui.error(six.text_type(exc))

    return response