How do I add a SOAP Header to a request ?

mikeyp
2008-02-14
2013-05-02
  • mikeyp
    mikeyp
    2008-02-14

    I'm trying to figure out how to add a SOAP header to a request using ZSI and a set of classes generated using wsdl2py. I"m using ZSI 2.0

    I'm connecting to SalesForce.com, which requires the SOAP headers to include a sessionID returned from the login call.

    My code looks like this:
    ------------------
    from SforceService_services import *
    # generated using wsdl2py agains the salesforce enterprise wsdl

    fp = file("tracefile","w")
    loc = SforceServiceLocator()
    sfdc = loc.getSoap(tracefile=fp)

    request = loginRequest()
    request.Username = 'mikeyp@snaplogic.org'
    request.Password = 'password' + 'key'

    response = sfdc.login (request)

    print response.Result.ServerUrl
    print response.Result.SessionId

    sfdc.binding.url=response.Result.ServerUrl
    # folowing throws a session id error
    #sfdc.binding.setURL(response.Result.ServerUrl)
    sfdc.binding.AddHeader('sessionId', response.Result.SessionId)

    request2 = describeGlobalRequest()
    response2 = sfdc.describeGlobal(request2)

    -----------------

    The call to describeGlobal() fails with :
    Traceback (most recent call last):
      File "sfinfo.py", line 23, in <module>
        response2 = sfdc.describeGlobal(request2)
      File "/Users/mikeyp/Documents/nico_local/Demo/SalesForce/python_ws/SforceService_services.py", line 72, in describeGlobal
        response = self.binding.Receive(describeGlobalResponse.typecode)
      File "build/bdist.macosx-10.3-fat/egg/ZSI/client.py", line 503, in Receive
      File "build/bdist.macosx-10.3-fat/egg/ZSI/client.py", line 431, in Receive
    ZSI.FaultException: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session

    I just figured out that Binding.AddHeader() adds an http header, not a soap header :-(

    Looking at ZSI/client.py, there doesn't seem to be a Binding() or _Binding() interface to set any SOAP headers. Am I missing something somewhere ?

    Thinking ahead, my next question will probably be how to set the namespace for that header, if thats needed...
    Thanks,
    mike

     
    • mikeyp
      mikeyp
      2008-02-14

      Never mind, a second search picked up answers, I"ll try those.

       
    • Fab
      Fab
      2008-06-03

      Hi,

      I've the same difficulty to add custom header into a soap message.

      Did you find the solution ?

      My code is something like that :
      ------------------------------------------------------------

      def sendSubscribe():

      ....

      req = SubscribeRequest()
                  req.Address = "127.0.0.1"
                  req.Expires = dt           
                  req.Filter = "Filter"
      ....

      req_header = SubscribeRequestHeader()
                  req_header.Action = "Subscribe"
                  req_header.MessageID = "10"
                  req_header.To = "127.0.0.1" 
      ....

      resp = enforcementServerPortType.Subscribe(req, req_header)

      -----------------------------------------------------

      The Subcribe method make a Send :

      -----------------------------------------------------
      ....

      self.binding.Send(None,None,req,soapaction="",soapheaders=req_header)

      ....

      -----------------------------------------------------

      The req_header didn't add anything to the header when the request is sent. I define soapheaders as a keyword that make the zeralize method of ZSI.

      Thanks for your help,

      Fab