通过带有不同名称的多个标签的zeep请求

时间:2019-10-15 13:30:59

标签: python zeep

晚上好!我为我的英语不好立即道歉。

我在通过zeep发送具有相同名称的嵌套字段时遇到问题,zeep忽略所有字段,仅发送最后一个。

我对zeep库有这样的功能

def send_data(self, data):
    response = self.client.service.GetAddrLetter(
       Key = '12345',
       AddrInfo={'RcpnName': data['recipient_name'],
                 'RcpnIndex': data['recipient_index'],
                 'RcpnPhone': data['recipient_phone'],
                 'RcpnEmail': 'my@mail.mail',
                 'RcpnStreet': data['recipient_address'],
                 'SndrName': data['sender_name'],
                 'SndrPhone': data['sender_phone'],
                 'SndrIndex': data['sender_index'],
                 'SndrStreet': data['sender_address'],
                 'AddInfo':{'Field': data['request_order'], 'Field': data['old_order'], 'Field': data['new_order']},               
        )
        return response

数据是一个数据字典,我从中获取值并将其发送到服务。

工作请求看起来像这样,可以通过浏览器或soapui发送此请求,所有字段都得到了完美处理。

<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:pos="blabla">
    <soapenv:Header/>
   <soapenv:Body>
        <pos:GetAddrLetterRequest>
            <pos:Key>1234567</pos:Key>
         <pos:AddrInfo>
            <!--Optional:-->
            <pos:RcpnName>Name Name</pos:RcpnName>
            <pos:RcpnPhone>123456</pos:RcpnPhone>
            <!--Optional:-->
            <pos:RcpnEmail>mail@mail</pos:RcpnEmail>
            <pos:RcpnStreet>my street</pos:RcpnStreet>
            <pos:SndrName>shop_name</pos:SndrName>
            <pos:SndrPhone>123456</pos:SndrPhone>
            <pos:SndrStreet>my street</pos:SndrStreet>
            <!--Optional:-->
            <pos:AddInfo>
            <!--0 to 5 repetitions:-->
               <pos:Field>111</pos:Field>
               <pos:Field>222</pos:Field>
              <pos:Field>333</pos:Field>
            </pos:AddInfo>
         </pos:AddrInfo>
      </pos:GetAddrLetterRequest>
   </soapenv:Body>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:1)

晚上好,经过长时间的折磨,我解决了这个问题,我不能说我的解决方案是优雅还是正确的,但是它可行,这对我来说现在是最重要的。

我在库中研究了手册的this part,但可惜的是,嵌套标签的解决方案都没有帮助我

然后,我执行以下操作,我只是从字典中拉出了所需的字段,并使用串联将其添加到所需的字段中。

def send_data(self, data):
    request_order = str(data['request_order'])
    old_order = str(data['old_order'])
    new_order = str(data['new_order'])
    field_value = request_order + '\n' + old_order + '\n' + new_order
    response = self.client.service.GetAddrLetter(
       Key = '12345',
       AddrInfo={'RcpnName': data['recipient_name'],
                 'RcpnIndex': data['recipient_index'],
                 'RcpnPhone': data['recipient_phone'],
                 'RcpnEmail': 'my@mail.mail',
                 'RcpnStreet': data['recipient_address'],
                 'SndrName': data['sender_name'],
                 'SndrPhone': data['sender_phone'],
                 'SndrIndex': data['sender_index'],
                 'SndrStreet': data['sender_address'],
                 'AddInfo':{'Field': field_value },               
        )
        return response

现在一切正常,对于错误的代码,我表示歉意,我只是一个初级开发人员,希望这对某人有用

答案 1 :(得分:0)

有一种方法可以添加为数组。 zeep 会将数组值转换为重复元素格式。

field_value = [111,222,333]

然后 zeep 会将其视为

<pos:AddInfo>
           <pos:Field>111</pos:Field>
           <pos:Field>222</pos:Field>
          <pos:Field>333</pos:Field>
        </pos:AddInfo>