splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

Issue with HTTPError and writing XML.

Open Hetang23 opened this issue 7 years ago • 4 comments

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.

Hetang23 avatar Sep 11 '19 10:09 Hetang23

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 avatar Sep 11 '19 10:09 Hetang23

@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 avatar Sep 11 '19 21:09 shakeelmohamed

@shakeelmohamed I've created the PR (#273 ) for the same.

Hetang23 avatar Sep 26 '19 05:09 Hetang23

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.

ashah-splunk avatar Oct 18 '21 16:10 ashah-splunk