Skip to content

UnicodeEncodeError when parsing rest response with version 1.6.3 and python 2.7 #198

@xcgspring

Description

@xcgspring

Below script works fine with version 1.6.2, but encounter UnicodeEncodeError issue with version 1.6.3, it should leave decoding the bytes to the ElementTree parser instead of decoding the bytes first, check https://stackoverflow.com/questions/21698024/how-to-correctly-parse-utf-8-xml-with-elementtree:

# encoding: utf-8

import time
from splunklib import client

service = client.connect(
    host='10.66.138.6',
    port=8089,
    username='admin',
    password='password',
    app='search',
    owner='nobody'
)

props_conf = service.confs['props']
if 'access_combined' not in props_conf:
    props_conf.create('access_combined')

stanza = props_conf['access_combined']

key = 'EVAL-バイト'
value = 'bytes'
if key not in stanza.content:
    stanza.submit({key: value})

time.sleep(5)
stanza = props_conf['access_combined']
for key, value in stanza.content.items():
    print key, value

Error log:

(venv)localhost:tmp splunk$ python test6.py 
Traceback (most recent call last):
  File "test6.py", line 27, in <module>
    stanza = props_conf['access_combined']
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 1222, in __getitem__
    entries = self._load_list(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 1326, in _load_list
    entries = _load_atom_entries(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 202, in _load_atom_entries
    r = _load_atom(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 197, in _load_atom
    return data.load(response.body.read().decode('utf-8'), match)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/data.py", line 79, in load
    root = XML(text)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1657, in feed
    self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3323-3325: ordinal not in range(128)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions