Python - Zeep SOAP Complex Header

前端 未结 2 1514
无人及你
无人及你 2021-02-06 12:44

I\'d like to pass \"Complex\" Header to a SOAP service with zeep library

Here\'s what it should look like

 
      

        
相关标签:
2条回答
  • 2021-02-06 13:16

    Thx Oblivion02.

    I finally use a raw method

    headers = {'content-type': 'text/xml'}
    body = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://blablabla">
    <soapenv:Header>
    <something:myVar1>FOO</something:myVar1>
    <something:myVar2>JAM</something:myVar2>
    </soapenv:Header>
    <soapenv:Body>
              ...
    </soapenv:Body>
    </soapenv:Envelope>"""
    
    response = requests.post(wsdl,data=body,headers=headers)
    
    0 讨论(0)
  • 2021-02-06 13:17

    I recently encountered this problem, and here is how I solved it.

    Say you have a 'Security' header that looks as follows...

    <env:Header>
    <Security>
        <UsernameToken>
            <Username>__USERNAME__</Username>
            <Password>__PWD__</Password>
        </UsernameToken>
        <ServiceAccessToken>        
            <AccessLicenseNumber>__KEY__</AccessLicenseNumber>
        </ServiceAccessToken>
    </Security>
    </env:Header>
    

    In order to send this header in the zeep client's request, you would need to do the following:

    header = zeep.xsd.Element(
                'Security',
                zeep.xsd.ComplexType([
                    zeep.xsd.Element(
                        'UsernameToken',
                        zeep.xsd.ComplexType([
                            zeep.xsd.Element('Username',zeep.xsd.String()),
                            zeep.xsd.Element('Password',zeep.xsd.String()),
                        ])
                    ),
                    zeep.xsd.Element(
                        'ServiceAccessToken',
                        zeep.xsd.ComplexType([
                            zeep.xsd.Element('AccessLicenseNumber',zeep.xsd.String()),
                        ])
                    ),
                ])
            )
    
    header_value = header(UsernameToken={'Username':'test_user','Password':'testing'},UPSServiceAccessToken={'AccessLicenseNumber':'test_pwd'})
    
    client.service.method_name_goes_here(
                        _soapheaders=[header_value],#other method data goes here
                    )
    
    0 讨论(0)
提交回复
热议问题