[英]How to translate XML post request to python
我需要将 C# 发布请求发送的XML
格式的参数转换为 python dictionary/json
格式。
这是xml
中的请求和参数:
SERVER_URL = http://www.server/DCTServer.aspx
<server>
<requests>
<Session.loginRq userName="user" password="123" />
<OnlineData.loadPolicyRq policyNumber=" 8800000093" />
<Session.getDocumentRq />
<Session.closeRq />
</requests>
</server>
这是我尝试过的:
import requests
url = 'http://www.server/DCTServer.aspx'
params = {
'server': {
'requests': {
'Session.loginRq': {
'userName': "user",
'password':'123'
},
'OnlineDataloadPolicyRq': {
'policyNumber': "8800000093"
}
}
}
}
我收到JSONDecodeError: Expecting value
错误。 我什至不确定我是否正确设置了参数。
这是我第一次不得不翻译(甚至阅读)C#。 提前感谢您提供的任何帮助。
我尝试使用https://jsonlint.com 验证您的 JSON 。 这是无效的,因为您可以在jQuery.parseJSON 单引号与双引号中看到单引号请尝试以下操作:
{
"server": {
"requests": {
"Session.loginRq": {
"userName": "user",
"password": "123"
},
"OnlineDataloadPolicyRq": {
"policyNumber": "8800000093"
}
}
}
}
验证 JSON 的地方。
In Python, could be useful, json.dumps on the server dictionary, or even better dicttoxml to get the correct xml ( https://pypi.org/project/dicttoxml/ ):
import json
import dicttoxml
params = {
'server': {
'requests': {
'Session.loginRq': {
'userName': 'user',
'password': '123'
},
'OnlineDataloadPolicyRq': {
'policyNumber': '8800000093'
}
}
}
}
print(json.dumps(params))
print(dicttoxml.dicttoxml(params, attr_type=False))
输出正确的 json。
我刚刚发现我可以将 xml 直接作为字符串发布:
import requests
SERVER_URL = http://www.server/DCTServer.aspx
xml = """<?xml version='1.0' encoding='utf-8'?>
<server>
<requests>
<Session.loginRq userName="user" password="123" />
<OnlineData.loadPolicyRq policyNumber="8800000093" />
<Session.getDocumentRq />
<Session.closeRq />
</requests>
</server>
"""
headers = {'Content-Type': 'application/xml'} # set what your server accepts
requests.post('http://faapcdd0799v.zurich.com:81/DuckCreek/DCTServer.aspx',
data=xml, headers=headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.