title: 'Exception while reading request', 'detail': 'Cannot decode: java.io.StringReader@1aac9ea'}, 'status': 'failure'}
body: <p>My first question is, what does "cannot decode: java.io.stringreader" mean?</p>
  
  <p>My second question is, why do some strings work and others do not? I'm guessing that there's an issue with python passing certain strings with d0rked encoding? </p>
  
  <p>My third question is, am I converting the XML values correctly before passing them to the REST body?</p>
  
  <p>I'm taking XML files from a directory, parsing the XML, then populating a REST post to a ServiceNow instance. Essentially I'm bringing a TON of legacy tickets from one system to another. I've solved the issues where special characters appear (like bullets). But for some reason, I keep getting the <code>Cannot decode: java.io.stringreader@bleh</code> error on some of the XML records, and not others. I've tried manually changing the text in that element (request) to just one word, that didn't work. So that leads me to believe that it has something to do with the encoding. In my response, I'm seeing that it was all sent in utf-8 as designed. I just don't understand what the issue is. And it's only with the request element. Everything else works perfectly.</p>
  
  <p>I've tried countless ways of changing the parsed XML to strings, text, encoding and decoding it. It's just strange that all the other XML elements that are converted to .text work flawlessly. It's just the request element.</p>
  
  <pre><code>def restMessage(number, subject, queue, opendate, closedate, request, history, final, category, subcategory):
  # Set the request parameters
      import requests
      url = 'https://*.service-now.com/api/now/import/sn_hr_core_mayo_case_import'
  
      # Eg. User name="admin", Password="admin" for this code sample.
      user = '*'
      pwd = '*'
  
      # Set proper headers
      headers = {"Content-Type":"application/json","Accept":"application/json"}
  
      # Build body
      body = "{\"u_state\":\"3\",\"u_opened_at\":\""+opendate+"\",\"u_closed_at\":\""+closedate+"\",\"u_source\":\"Self Service\",\"u_hr_service\":\"e2e4665cdb4f77003693f8fdbf96198b\",\"u_topic_category\":\"cec4bc2d9f931200d9011977677fcfe8\",\"u_opened_for\":\""+subject+"\",\"u_subject_person\":\""+subject+"\",\"u_assigned_to\":\"system\",\"u_queue\":\""+queue+"\",\"u_assignment_group\":\"f65370019f22120047a2d126c42e705c\",\"u_correlation_id\":\""+number+"\",\"u_request\":\""+request+"\",\"u_archive_category\":\""+category+"\",\"u_archive_subcategory\":\""+subcategory+"\"}"
  
      # Do the HTTP request
      response = requests.post(url, auth=(user, pwd), headers=headers ,data=body)
  
      # Check for HTTP codes other than 200
      #if response.status_code != 200:
      print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json())
      # Toggle exit if you want to only do one record.
      # exit()
      # Decode the JSON response into a dictionary and use the data
      data = response.json()
      print("Data from response = " + str(data))
  
  #---------parse XML files from directory-----------
  import os
  from xml.etree import ElementTree as ET
  
  # files are in a sub folder where this script is being ran
  path = "attachments"
  total = 0
  count = 0
  
  # Get the total number of xml records
  files = os.listdir(path)
  for x in files:
      if x.endswith(".xml"):
          total += 1
  
  for filename in os.listdir(path):
      # Only get xml files
      if filename.endswith(".xml"):
          fullname = os.path.join(path, filename)
      # This joins full path / filename to trigger parser
          tree = ET.parse(fullname)
          # print(etree.tostring(root, pretty_print=True))
          # from xml.dom.minidom import parse
          # dom = parse(fullname)
          # print('&lt;?xml version="{}" encoding="{}"?&gt;'.format(dom.version, dom.encoding))
  
          # Parse the files..
          # Build tags from all the child elements in root IE: /CASE
          print("tree Obj ID= " + str(tree))
          qnumber = tree.find("NUMBER")
          qstatus = tree.find("STATUS")
          qqueue = tree.find("QUEUE")
          qcategory = tree.find("CATEGORY")
          qsubcategory = tree.find("SUB_CATEGORY")
          qsubject = tree.find("USER_ID")
          qopendate = tree.find("OPEN_DATE")
          qclosedate = tree.find("CLOSE_DATE")
          qrequest = tree.find("REQUEST")
          qhistory = tree.find("HISTORY_RESPONSE")
          qfinal = tree.find("FINAL_RESPONSE")
  
          #Convert everything to strings
          number = qnumber.text
          status = qstatus.text
          category = qcategory.text
          subcategory = qsubcategory.text
          queue = qqueue.text
          subject = qsubject.text
          opendate = qopendate.text
          closedate = qclosedate.text
          request = qrequest.text
          history = qhistory.text
          final = qfinal.text
  
          # Convert Special Characters
          # bad_chars = ['•','/']
          # for i in bad_chars:
          #     request = request.replace(i, '-')
          # # request =  ''.join([i for i in p_request if i.isdigit()])
          # request.encode(encoding='UTF-8')
  
          print(request)
  
          #Print case number to assist with knowing where in count
          print("Case Number: " + number)
          count += 1
          print("Count: " + str(count))
          print("Total: " + str(total))
          restMessage(number, subject, queue, opendate, closedate, request, history, final, category, subcategory)
  </code></pre>
  
  <p>This is the response I'm getting on certain records:</p>
  
  <pre><code>Status: 400 Headers: {'error': {'message': 'Exception while reading request', 'detail': 'Cannot decode: java.io.StringReader@1aac9ea'}, 'status': 'failure'}
  </code></pre>
  
  <p>With the ones that work (for no apparent reason):</p>
  
  <pre><code>Status: 201 Headers: {'import_set': 'ISET0010005', 'staging_table': 'sn_hr_core_*_case_import', 'result': [{'transform_map': '* Case Import', 'table': 'sn_hr_core_case', 'display_name': 'number', 'display_value': 'HRC0001165', 'record_link': 'https://*.service-now.com/api/now/table/sn_hr_core_case/a3669014db8777003693f8fdbf9619b4', 'status': 'ignored', 'sys_id': 'a3669014db8777003693f8fdbf9619b4', 'status_message': 'No field values changed'}]}
  </code></pre>
  
  <p>I have removed some sensitive information for obvious reasons. You will see a * in that case.</p>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;CASE&gt;
    &lt;NUMBER&gt;20514217&lt;/NUMBER&gt;
    &lt;PARENT&gt;
    &lt;/PARENT&gt;
    &lt;STATUS&gt;1-CLOSED&lt;/STATUS&gt;
    &lt;OPEN_DATE&gt;12/01/2017 00:29:46&lt;/OPEN_DATE&gt;
    &lt;CLOSE_DATE&gt;12/05/2017 17:42:26&lt;/CLOSE_DATE&gt;
    &lt;SOURCE&gt;Self Service&lt;/SOURCE&gt;
    &lt;PROCESS&gt;HR INTERNAL REQUEST FORM&lt;/PROCESS&gt;
    &lt;CATEGORY&gt;HR Connect&lt;/CATEGORY&gt;
    &lt;SUB_CATEGORY&gt;Personnel Action Change/Update&lt;/SUB_CATEGORY&gt;
    &lt;USER_ID&gt;210140&lt;/USER_ID&gt;
    &lt;LAST_NAME&gt;Mickey mouse&lt;/LAST_NAME&gt;
    &lt;FIRST_NAME&gt;Mickey mouse&lt;/FIRST_NAME&gt;
    &lt;SITUATION&gt;SELECT...&lt;/SITUATION&gt;
    &lt;PRIORITY&gt;5 Days&lt;/PRIORITY&gt;
    &lt;ADVISOR_NAME&gt;Donald Duck&lt;/ADVISOR_NAME&gt;
    &lt;TEAM&gt;2 HR SRV CNTR PA&lt;/TEAM&gt;
    &lt;NEXT_ACTION&gt;
    &lt;/NEXT_ACTION&gt;
    &lt;PROCESS_STATUS&gt;Verified&lt;/PROCESS_STATUS&gt;
    &lt;TRANSFERT_DATE&gt;
    &lt;/TRANSFERT_DATE&gt;
    &lt;DEADLINE&gt;12/12/2017 17:07:12&lt;/DEADLINE&gt;
    &lt;QUEUE&gt;HR Internal Request&lt;/QUEUE&gt;
    &lt;FROZEN_DATE&gt;
    &lt;/FROZEN_DATE&gt;
    &lt;OTHER_EMPLOYEE_ID&gt;210140&lt;/OTHER_EMPLOYEE_ID&gt;
    &lt;REQUEST&gt;Please complete a multi-add to this employee's record for the 11-29-2017 pay period. See attached request form with all pertinent PA details.
  
  Thank you,
  Donald Duck&lt;/REQUEST&gt;
    &lt;HISTORY_RESPONSE&gt;[2017-12-01 17:52:09 - Donald Duck] Thank you for contacting HR Connect, your request for Mickey mouse has been processed.
  
  [2017-12-01 17:52:10 - Donald Duck] Thank you for contacting HR Connect, your request for Melissa A. Gilbertson has been processed.&lt;/HISTORY_RESPONSE&gt;
    &lt;FINAL_RESPONSE&gt;Thank you for contacting HR Connect, your request for Mickey mouse has been processed.&lt;/FINAL_RESPONSE&gt;
  &lt;/CASE&gt;
  </code></pre>
  
  <p>--I've added a screenshot of what I think the problem is. Looks like the ServiceNow json parser doesn't like \n 's? (return carriages).. I will post more as I work towards the solution.</p>
  
  <p><a href="https://i.stack.imgur.com/AwRGG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AwRGG.png" alt="enter image description here"></a></p>