How to set soap headers in zeep when header has multiple elements

∥☆過路亽.° 提交于 2019-12-05 03:40:30

问题


I am trying to use python zeep to connect to a soap service ( using wsdl ).

Following is the soap-ui generated XML for an operation.

However I am finding it difficult to identify how to set soap headers. In this case, we have multiple XML elements within the header.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:acm="http://www.acme.com/ACM">
       <soapenv:Header>
          <acm:MessageID>?</acm:MessageID>
          <acm:ExName>?</acm:ExName>
          <acm:Authentication>
             <acm:Username>?</acm:Username>
             <acm:Password>?</acm:Password>
          </acm:Authentication>
       </soapenv:Header>
       <soapenv:Body>
          <acm:LIST_STOCKS>
             <!--Optional:-->
             <acm:STOCKID>?</acm:STOCKID>
             <!--Optional:-->
             <acm:PRODUCT>?</acm:PRODUCT>
          </acm:LIST_STOCKS>
       </soapenv:Body>
    </soapenv:Envelope>

Thanks.


回答1:


This is an old question, but I'll leave an answer here for future reference.

It is not that clear from the documentation, but you can just set the elements by setting _soap_headers with a dictionary.

In the given example:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:acm="http://www.acme.com/ACM">
       <soapenv:Header>
          <acm:MessageID>?</acm:MessageID>
          <acm:ExName>?</acm:ExName>
          <acm:Authentication>
             <acm:Username>?</acm:Username>
             <acm:Password>?</acm:Password>
          </acm:Authentication>
       </soapenv:Header>
       <soapenv:Body>
          <acm:LIST_STOCKS>
             <!--Optional:-->
             <acm:STOCKID>?</acm:STOCKID>
             <!--Optional:-->
             <acm:PRODUCT>?</acm:PRODUCT>
          </acm:LIST_STOCKS>
       </soapenv:Body>
    </soapenv:Envelope>

You would send the headers like this:

# Prepare header values and dicts
MessageID = 000
ExName = 'Value'
Authentication = {'Username': 'User', 'Password': 'YourPassword'}

# Set required body content
LIST_STOCKS = [] 

# Call service and set SOAP headers directly in _soapheaders using dictionary
response = 
self.client.service.WebServiceName(_soapheaders={'MessageID': MessageID, 'ExName': ExName, 'Authentication': Authentication},LIST_STOCKS=LIST_STOCKS)


来源:https://stackoverflow.com/questions/46410844/how-to-set-soap-headers-in-zeep-when-header-has-multiple-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!