简体   繁体   English

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

[英]How to use zeep to make SOAP requests in python

here is my xml: 这是我的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>

here is my demo: 这是我的演示:

wsdl = ''

client = Client(
    wsdl,
    wsse=UsernameToken('USERNAME', '1234'))

response = client.service.get_method(
    EMAIL='david')

it raised VadlidationError: 它引发了VadlidationError:

ValidationError: Missing element OPRID (root.OPRID)

I don't know why, please give me some help, thanks. 我不知道为什么,请给我一些帮助,谢谢。

zeep is advanced library to handle SOAP communications in python. zeep是用于处理python中SOAP通信的高级库。 You should provide wsdl file, so that your issue can be better analyzed. 您应该提供wsdl文件,以便可以更好地分析您的问题。

But by looking into the xml request you have provided, it seems the authentication is been done using headers and data is been sent in body. 但是通过查看您提供的xml请求,似乎认证是使用标头完成的,并且数据是在正文中发送的。 Similar to the usecase i have recently fixed. 与我最近修复的用例相似。 Refer my xml request of my use case below. 请在下面参考我的用例的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>

For the above xml, the code is as follows 对于上述xml,代码如下

from zeep import Client
header_credentials = {'username':'xxxxx','password':'xxxxx'}
tac_data = {'name': {'title':'xxxxx','forename':'xxxxx','surname':'xxxxx'}}

client = Client(wsdl=wsdl)
response = client.service.Search02c(tac_data, _soapheaders={'callcreditheaders':header_credentials})

In the above code, "Search02c" is the operation name for the service. 在上面的代码中,“ Search02c”是服务的操作名称。 Operation name can be found while inspecting the wsdl file. 在检查wsdl文件时可以找到操作名称。 In my usecase "Search02c" accepts 2 arguments which are body and header."tac_data" is the dictionary of the xml body(not header) and "header_credentials" is the dictionary of the credentials. 在我的用例中,“ Search02c”接受2个参数,分别是正文和标头。“ tac_data”是xml正文(不是标头)的字典,“ header_credentials”是凭据的字典。 Your use case might accept single argument clubbing header and body. 您的用例可能接受单参数合并标头和正文。 The arguments structure can be found after operation name in the inspected wsdl file. 可以在检查的wsdl文件中的操作名称后找到参数结构。

You can find the operation name and its structure in the end of the output by running this in your command prompt. 通过在命令提示符下运行此操作,可以在输出末尾找到操作名称及其结构。

python -mzeep wsdl_file_path.wsdl

The operation for my wsdl file is below. 我的wsdl文件的操作如下。

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

Remember, zeep only accepts dictionary as input data and provides dictionary as output. 请记住,zeep仅接受字典作为输入数据,并提供字典作为输出。 If you like to receive response as xml, use raw_response=True in the client settings. 如果您希望以xml格式接收响应,请在客户端设置中使用raw_response = True。

For more information, please refer zeep documentation 有关更多信息,请参阅zeep文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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