2017-03-28 88 views
0

我想發送請求到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() 

回答

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)