0

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.

Jordan
  • 1,415
  • 3
  • 18
  • 44

2 Answers2

0

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.

  • Thank you for the response. I still get the same error. Did I set the dictionary up correctly according to the c# code? – Jordan Dec 17 '20 at 12:57
  • The snippet you posted is not properly c#, it's an xml snippet, is it part of an ASP.NET web site? – Danilo Cataldo Dec 17 '20 at 13:03
  • Got it. I didn't know. It's a internal system my work uses. Not sure what ASP.NET is to be honest. I'll edit the question. – Jordan Dec 17 '20 at 13:13
0

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)
Jordan
  • 1,415
  • 3
  • 18
  • 44