简体   繁体   English

如何将 XML 发布请求翻译为 python

[英]How to translate XML post request to python

I need to translate parameters in an XML format sent by a C# post request to python dictionary/json format.我需要将 C# 发布请求发送的XML格式的参数转换为 python dictionary/json格式。

Here is the request and parameters in xml :这是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.我收到JSONDecodeError: Expecting value错误。 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#.这是我第一次不得不翻译(甚至阅读)C#。 Thanks in advance for any help you can provide.提前感谢您提供的任何帮助。

I've tried to validate your JSON with https://jsonlint.com .我尝试使用https://jsonlint.com 验证您的 JSON It is not valid because the single quotes as you can see at jQuery.parseJSON single quote vs double quote Please try this:这是无效的,因为您可以在jQuery.parseJSON 单引号与双引号中看到单引号请尝试以下操作:

{
    "server": {
        "requests": {
            "Session.loginRq": {
                "userName": "user",
                "password": "123"
            },
            "OnlineDataloadPolicyRq": {
                "policyNumber": "8800000093"
            }
        }
    }
}

Where the JSON is validated.验证 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/ ): 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.输出正确的 json。

I just figured out I could post the xml directly as as a string:我刚刚发现我可以将 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM