当标头包含多个元素时,如何在zeep中设置soap标头 [英] How to set soap headers in zeep when header has multiple elements

查看:182
本文介绍了当标头包含多个元素时,如何在zeep中设置soap标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python zeep连接到soap服务(使用wsdl).

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

以下是soap-ui为操作生成的XML.

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

但是我发现难以确定如何设置肥皂标头. 在这种情况下,我们在标头中包含 多个XML元素 .

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>

谢谢.

推荐答案

这是一个老问题,但我将在此处留下答案以供将来参考.

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

从文档中还不清楚,但是您可以通过使用字典设置_soap_headers来设置元素.

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

在给定的示例中:

<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>

您将像这样发送标头:

# 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)

这篇关于当标头包含多个元素时,如何在zeep中设置soap标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆