Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default encoding - lantin-1 to send_file_request method #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion plugins/httpapi/nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import traceback
import mimetypes
import tempfile
import sys
from ansible.module_utils.six import PY3
from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.connection import ConnectionError
Expand Down Expand Up @@ -261,7 +262,22 @@ def send_file_request(self, method, path, file=None, data=dict(), remote_path=No
self.nd.fail_json(msg="Cannot use requests_toolbelt MultipartEncoder() because requests_toolbelt module is not available")
mp_encoder = MultipartEncoder(fields=fields)
multiheader = {"Content-Type": mp_encoder.content_type, "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br"}
response, rdata = self.connection.send(path, mp_encoder.to_string(), method=method, headers=multiheader)

if sys.version_info.major == 2:
# For Python 2+
py2_default_encoding = sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding("latin-1") # To handle UnicodeDecodeError exception during the send function call

mp_data = mp_encoder.to_string()
response, rdata = self.connection.send(path, mp_data, method=method, headers=multiheader)

# Resetting it to the default encoding
if sys.version_info.major == 2:
# For Python 2+
reload(sys)
sys.setdefaultencoding(py2_default_encoding)

except Exception as e:
self.error = dict(code=self.status, message="ND HTTPAPI MultipartEncoder Exception: {0} - {1} ".format(e, traceback.format_exc()))
raise ConnectionError(json.dumps(self._verify_response(None, method, path, None)))
Expand Down