如何为肥皂肥皂客户端正确设置标头

时间:2017-03-28 19:50:43

标签: python soap client suds

我正在尝试使用suds客户端向api发送请求。

请求形成如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="https://API">
   <soapenv:Header>
      <v20:apiHeader>
         <v20:username>username</v20:username>
         <v20:password>password</v20:password>
         <!--Optional:-->
         <v20:name>?</v20:name>
      </v20:apiHeader>
   </soapenv:Header>
   <soapenv:Body>
      <v20:getLoginDetails/>
   </soapenv:Body>
</soapenv:Envelope>

我发送的请求如下:

client = Client('https://wsdl', faults=False)

client.set_options(headers={'username': username 'authToken': auth_token, 'name': ''})
client.service.getLoginDetails()

我收到的错误是:

(<HTTPStatus.INTERNAL_SERVER_ERROR: 500>, (Fault){
   faultcode = "soap:Server"
   faultstring = "apiHeader should not be null"
   detail = ""
 })

我应该如何发送请求?这绝对与我认为的apiHeader有关;不知道是什么,我使用这个得到同样的错误:

username = Element('username').setText(name)
        password= Element('password').setText(pass)
        header_list = [username, pass]
        self.client.set_options(soapheaders=header_list)
        return self.client.service.getLoginDetails()

2 个答案:

答案 0 :(得分:0)

我通过使用类似的东西来完成这项工作:

from suds.sax.element import Element
from suds.sax.attribute import Attribute
code = Element('serviceCode').setText('PABB4BEIJING')
pwd = Element('servicePwd').setText('QWERTPABB')

reqsoapheader = Element('ReqSOAPHeader').insert(code)
reqsoap_attribute = Attribute('xmlns', "http://schemas.acme.eu/")
reqsoapheader.append(reqsoap_attribute)

client.set_options(soapheaders=reqsoapheader)

答案 1 :(得分:0)

如果要在soap标头中添加几个元素:

userName = Element('UserName').setText(config['fco.user'])
password = Element('Password').setText(config['fco.pwd'])
fdxns = Attribute('xmlns', "http://fdx.co.il/Authentication")
for field in userName, password:
    field.append(fdxns)
soapheaders = [userName, password]
client.set_options(soapheaders=tuple(soapheaders))

这对我来说适用于python 2.7,suds 0.4。 祝你好运!