如何使用zeep在python中发出SOAP请求

2024-05-19 10:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的xml:

<?xml version="1.0"?>
<soapenv:Envelope>
  <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <wsse:Security soap:mustUnderstand="1" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>USERNAME</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
  <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <root xmlns="http://xmlns.oracle.com/Enterprise/tools/schema/InfoRtRequest.v1">
      <EMAIL>david</EMAIL>
    </root>
  </soapenv:Body>
</soapenv:Envelope>

下面是我的演示:

^{pr2}$

它引发了VadlidationError:

ValidationError: Missing element OPRID (root.OPRID)

我不知道为什么,请帮我一下,谢谢。在


Tags: orghttpdocsrootxmlopenschemassoap
1条回答
网友
1楼 · 发布于 2024-05-19 10:29:24

zeep是用python处理SOAP通信的高级库。您应该提供wsdl文件,以便更好地分析您的问题。

但通过查看您提供的xml请求,似乎验证是使用头完成的,而数据是在主体中发送的。类似于我最近修复的用例。请参阅下面我的用例的xml请求。

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <ns0:myheaders xmlns:ns0="xxxxxx_stackoverflow_mask_xxxxxx">
            <ns0:username>xxxxxx_stackoverflow_mask_xxxxxx</ns0:username>
            <ns0:password>xxxxxx_stackoverflow_mask_xxxxxx</ns0:password>
        </ns0:myheaders>
    </soap-env:Header>
    <soap-env:Body>
        <ns0:Search02c xmlns:ns0="xxxxxx_stackoverflow_mask_xxxxxx">
            <ns0:name>
                <ns0:title>Mr</ns0:title>
                <ns0:forename>Srikanth</ns0:forename>
                <ns0:surname>Badveli</ns0:surname>
            </ns0:name>
        </ns0:Search02c>
    </soap-env:Body>
</soap-env:Envelope>

对于上面的xml,代码如下

^{pr2}$

在上面的代码中,“Search02c”是服务的操作名。检查wsdl文件时可以找到操作名。在我的用例中,“Search02c”接受两个参数,即body和header。“tac_data”是xml正文的字典(不是头),“header_credentials”是凭证的字典。您的用例可能接受单参数的clubing头和body。参数结构可以在所检查的wsdl文件中的操作名之后找到。

通过在命令提示符下运行,可以在输出的末尾找到操作名及其结构。

python -mzeep wsdl_file_path.wsdl

我的wsdl文件的操作如下。

Operations:
    Search02c(searchDefinition: tac_data, _soapheaders={'headers': header_credentials}) -> outputResult: ns1:output

记住,zeep只接受dictionary作为输入数据,并提供dictionary作为输出。如果希望以xml形式接收响应,请在客户端设置中使用raw_response=True。

有关详细信息,请参阅zeep documentation

相关问题 更多 >

    热门问题