You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Python Zeep调用SOAP服务时的500错误及空XML问题

问题:Python Zeep调用西联SOAP服务触发500错误,提示XML内容无效(文档为空)

问题详情

使用Python Zeep调用西联SOAP服务时,发起请求后触发500错误,系统提示缺少参数,但已核对所有必填参数,问题仍未解决。同时错误日志显示收到无效XML内容(文档为空)。

请求报文结构

message = {
    'channel': {
        'type': 'f12',
        'name': 'ESP',
        'version': '5500'
    },
    'foreign_remote_system': {
        'identifier': 'W',
        'reference_no': '82',
        'counter_id': 'DV'
    },
    'payment_transaction': {
        'payment_details': {
            'destination_country_currency': {
                'iso_code': {
                    'country_code': 'DO',
                    'currency_code': currency_code
                }
            }
        },
        'mtcn': mtcn
    }
}

WSDL请求XML示例

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xrsi="http://www.westernunion.com/schema/xrsi">
<soapenv:Header/>
<soapenv:Body>
<xrsi:receive-money-search-request>
<channel>
<type>H</type>
<name>P</name>
<version>00</version>
</channel>
<foreign_remote_system>
<identifier>WG</identifier>
<reference_no>82</reference_no>
<counter_id>D</counter_id>
</foreign_remote_system>
<payment_transaction>
<payment_details>
<destination_country_currency>
<iso_code>
<country_code>u</country_code>
<currency_code>P</currency_code>
</iso_code>
</destination_country_currency>
</payment_details>
<mtcn>9</mtcn>
</payment_transaction>
</xrsi:receive-money-search-request>
</soapenv:Body>
</soapenv:Envelope>

错误信息

zeep.transports: Loading remote data from: https://wugateway2pi.westernunion.net?wsdl
ERROR:root:Exception occurred: Invalid XML content received (Document is empty, line 1, column 1)
127.0.0.1 - - [12/Jul/2023 09:07:55] "POST /receive_money/search/DOP/9735896579 HTTP/1.1" 500 -
INFO:werkzeug:127.0.0.1 - - [12/Jul/2023 09:07:55] "POST /receive_money/search/DOP/9735896579 HTTP/1.1" 500 -

排查与解决建议

1. 开启Zeep调试日志,捕获实际发送的XML报文

Zeep默认不输出完整请求XML,需开启调试模式,确认生成的XML是否符合WSDL要求:

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('zeep')
logger.setLevel(logging.DEBUG)

对比生成的XML和官方示例,重点检查:

  • 命名空间是否正确(xrsi前缀需对应http://www.westernunion.com/schema/xrsi)
  • 参数值的大小写、格式是否匹配接口规范(比如示例中channel.type为H,当前用f12需确认合法性)
  • 嵌套结构是否完整,无标签遗漏

2. 检查参数变量的实际值

“缺少参数”提示可能源于currency_code或mtcn变量为空,添加日志打印确认:

print(f"currency_code: {currency_code}, mtcn: {mtcn}")

确保变量有合法非空值,且符合西联接口格式要求(比如MTCN需为9位数字)。

3. 校验WSDL加载完整性

远程加载WSDL可能因网络问题导致解析异常,可下载WSDL到本地后初始化客户端:

from zeep import Client
client = Client('path/to/local/wsdl_file.wsdl')

4. 检查SOAP头是否需要额外认证

官方示例有<soapenv:Header/>空标签,但部分SOAP服务要求在Header中添加认证信息,查看西联文档后补充:

header = {
    'AuthHeader': {
        'Username': 'your_username',
        'Password': 'your_password'
    }
}
client.service.receive-money-search-request(_soapheaders=header, **message)

5. 用curl直接测试接口

绕开Zeep,手动构造XML用curl发送,确认接口本身是否正常:

curl -X POST \
  https://wugateway2pi.westernunion.net \
  -H 'Content-Type: text/xml; charset=utf-8' \
  -d '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xrsi="http://www.westernunion.com/schema/xrsi">
<soapenv:Header/>
<soapenv:Body>
<xrsi:receive-money-search-request>
<channel>
<type>f12</type>
<name>ESP</name>
<version>5500</version>
</channel>
<foreign_remote_system>
<identifier>W</identifier>
<reference_no>82</reference_no>
<counter_id>DV</counter_id>
</foreign_remote_system>
<payment_transaction>
<payment_details>
<destination_country_currency>
<iso_code>
<country_code>DO</country_code>
<currency_code>DOP</currency_code>
</iso_code>
</destination_country_currency>
</payment_details>
<mtcn>9735896579</mtcn>
</payment_transaction>
</xrsi:receive-money-search-request>
</soapenv:Body>
</soapenv:Envelope>'

若curl同样返回500,问题出在参数或接口权限;若curl成功,则说明Zeep的XML生成存在问题,需调整报文结构。


内容的提问来源于stack exchange,提问作者Joshua Flores

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 10:27:39