I need to translate parameters in an XML
format sent by a C# post request to python dictionary/json
format.
Here is the request and parameters in 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>
This is what I tried:
import requests
url = 'http://www.server/DCTServer.aspx'
params = {
'server': {
'requests': {
'Session.loginRq': {
'userName': "user",
'password':'123'
},
'OnlineDataloadPolicyRq': {
'policyNumber': "8800000093"
}
}
}
}
I'm getting JSONDecodeError: Expecting value
error. I'm not even sure i'm setting the parameters up correctly.
This is the first time I've had to translate (or even read) C#. Thanks in advance for any help you can provide.
I've tried to validate your JSON with https://jsonlint.com . It is not valid because the single quotes as you can see at jQuery.parseJSON single quote vs double quote Please try this:
{
"server": {
"requests": {
"Session.loginRq": {
"userName": "user",
"password": "123"
},
"OnlineDataloadPolicyRq": {
"policyNumber": "8800000093"
}
}
}
}
Where the JSON is validated.
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))
that outputs the correct json.
I just figured out I could post the xml directly as as a string:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.