问题描述
我的第一个问题是,无法解码:java.io.stringreader"是什么意思?
My first question is, what does "cannot decode: java.io.stringreader" mean?
我的第二个问题是,为什么有些字符串可以工作,而另一些却不能呢?我猜想python使用d0rked编码传递某些字符串存在问题吗?
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?
我的第三个问题是,在将XML值传递到REST主体之前,是否可以正确转换XML值?
My third question is, am I converting the XML values correctly before passing them to the REST body?
我从目录中获取XML文件,解析XML,然后将REST帖子填充到ServiceNow实例.本质上,我是将一堆旧式票据从一个系统带到另一个系统.我已经解决了出现特殊字符(例如项目符号)的问题.但是由于某些原因,我在某些XML记录上一直出现无法解码:java.io.stringreader@bleh
错误,而在其他XML记录上却没有.我尝试过手动将该元素(请求)中的文本更改为一个单词,但没有用.因此,我相信它与编码有关.在我的答复中,我看到它全部按设计以utf-8发送.我只是不明白问题是什么.它仅与request元素一起使用.其他一切都工作正常.
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 Cannot decode: java.io.stringreader@bleh
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.
我尝试了无数种将解析后的XML更改为字符串,文本,对其进行编码和解码的方法.奇怪的是,所有其他转换为.text的XML元素都可以正常工作.只是请求元素.
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.
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('<?xml version="{}" encoding="{}"?>'.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)
这是我在某些记录中得到的答复:
This is the response I'm getting on certain records:
Status: 400 Headers: {'error': {'message': 'Exception while reading request', 'detail': 'Cannot decode: java.io.StringReader@1aac9ea'}, 'status': 'failure'}
使用有效的方法(没有明显的原因):
With the ones that work (for no apparent reason):
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'}]}
出于明显的原因,我删除了一些敏感信息.在这种情况下,您会看到一个*.
I have removed some sensitive information for obvious reasons. You will see a * in that case.
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<CASE>
<NUMBER>20514217</NUMBER>
<PARENT>
</PARENT>
<STATUS>1-CLOSED</STATUS>
<OPEN_DATE>12/01/2017 00:29:46</OPEN_DATE>
<CLOSE_DATE>12/05/2017 17:42:26</CLOSE_DATE>
<SOURCE>Self Service</SOURCE>
<PROCESS>HR INTERNAL REQUEST FORM</PROCESS>
<CATEGORY>HR Connect</CATEGORY>
<SUB_CATEGORY>Personnel Action Change/Update</SUB_CATEGORY>
<USER_ID>210140</USER_ID>
<LAST_NAME>Mickey mouse</LAST_NAME>
<FIRST_NAME>Mickey mouse</FIRST_NAME>
<SITUATION>SELECT...</SITUATION>
<PRIORITY>5 Days</PRIORITY>
<ADVISOR_NAME>Donald Duck</ADVISOR_NAME>
<TEAM>2 HR SRV CNTR PA</TEAM>
<NEXT_ACTION>
</NEXT_ACTION>
<PROCESS_STATUS>Verified</PROCESS_STATUS>
<TRANSFERT_DATE>
</TRANSFERT_DATE>
<DEADLINE>12/12/2017 17:07:12</DEADLINE>
<QUEUE>HR Internal Request</QUEUE>
<FROZEN_DATE>
</FROZEN_DATE>
<OTHER_EMPLOYEE_ID>210140</OTHER_EMPLOYEE_ID>
<REQUEST>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</REQUEST>
<HISTORY_RESPONSE>[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.</HISTORY_RESPONSE>
<FINAL_RESPONSE>Thank you for contacting HR Connect, your request for Mickey mouse has been processed.</FINAL_RESPONSE>
</CASE>
-我已经添加了我认为问题所在的屏幕截图.看起来ServiceNow json解析器不喜欢\ n吗?(回车)..在为解决问题而努力的过程中,我将发布更多内容.
--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.
推荐答案
最终导致源xml记录中的编码出现问题.Python无法翻译大量奇怪的垃圾字符.我创建了一种去除奇数字符的方法.这是我使用的代码...
This ended up being an issue with the encoding in the source xml records. Python couldn't translate a ton of weird junk characters. I created a way to strip out the odd characters. Here's the code I used...
def fix_multi_line_string(text):
'''Given a string input,
replace all unwanted characters with desginated replacements.
Return the fixed string.'''
char_map = {
'•': '-',
'\t': '',
'\n': '',
'\r': '',
'\u200b': '',
'"': "'",
'\ufffd': '',
'\uf0b7': '',
'\uf0a7': '',
'\uf071': '',
'\u25e6': '',
'\uf020': '',
'\u2265': '',
'\u200e': '',
'\uf0d8': '',
'\u2010': '',
'\uf04a': '',
'\u25fe': '',
'\u2610': '',
'\u2015': '',
'\u2612': '',
'\uf04c': '',
'\uf15c': '',
'\ue1ef': '',
'\u25b2': '',
'\U0001f609': '',
'\u2502': '',
'\uf02a': '',
'\uf0e0': '',
'\uf028': '',
'\u25a1': '',
'\ue0e8': '',
'\u2003': '',
'\uf0a0': '',
'\ue900': '',
'\ue901': '',
'\ue076': '',
'\ue1d9': '',
'\ue1ed': '',
'\u25bc': '',
'\uf073': '',
'\ue099': '',
'\uf0fc': '',
'\uf0df': '',
'\uff08': '',
'\u2002': '',
'\uf0e8': '',
'\uf07f': '',
'\uf06f': '',
'\u263a': '',
'\ue1b4': '',
'\ue1b4': '',
'\u2500': '',
'\uf084': '',
'\ue0ca': '',
'\U00100078': '',
'\uf027': '',
'\uf0ac': '',
'\U0001f604': '',
'\uff1f': '',
'\uff0c': '',
'\uff01': '',
'\u2219': '',
'\u2219': '',
'\U0001f60a': '',
'\uf0a8': '',
'\ue172': '',
'\ue080': '',
'\ue113': '',
'\ue1bc': '',
'\ue202': '',
'\u2190': '',
'\u2192': '',
'\uf076': '',
'\ue021': '',
'\uf06e': '',
'\uf030': '',
'\uf077': '',
'\uf111': '',
'\uf12a': '',
'\ue22a': '',
'\u2981': '',
'\uf02d': '',
'\uf037': '',
'///': ''
}
for char in char_map:
text = text.replace(char, char_map[char])
return text
这篇关于“读取请求时发生异常",“详细信息":“无法解码:java.io.StringReader@1aac9ea",“状态":“失败"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!