Issue with HTTPError and writing XML.
While using the HTTPError (splunklib/binding.py) on Python3, the error shown is form of bytes. Example: b'{"messages":[{"type":"ERROR","text":"<the error message that is to be raised>"}]}'
And when using the write_xml_document() method (splunklib/modularinput/event_writer.py), we are recieving ERRORwrite() argument must be str, not bytes. on Python3 without any stacktrace.
As for the solution, in splunklib/binding.py for class HTTPError, we change the line body = response.body.read() to body = (response.body.read()).decode().
And, for write_xml_document(), we surrounded the _self.out.write(ET.tostring(document)) statement with try-except and in except block, added self._out.write(ET.tostring(document, encoding="unicode")), i.e.,
try:
self._out.write(ET.tostring(document))
except:
self._out.write(ET.tostring(document, encoding="unicode"))
@Hetang23 can you create a PR for this change? We probably won't get to look at it until after .conf however due to resourcing constraints
@shakeelmohamed I've created the PR (#273 ) for the same.
Hi @Hetang23 , appreciate your efforts for the PR. The latest release of python sdk supports Python3 along with Python2, so we would request you to use the latest version of python sdk
In the latest version, before writing the encoded XML document to stream, it is encoded through "ensure_str()" which handles the decoding of bytes before encoding the xml document while using Python3.
Closing the issue for now, please reopen the issue if you still face the issue.