How do I add a SOAP Header to a request ?

  • 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, 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 = ''
    request.Password = 'password' + 'key'

    response = sfdc.login (request)

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

    # folowing throws a session id error
    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 "", line 23, in <module>
        response2 = sfdc.describeGlobal(request2)
      File "/Users/mikeyp/Documents/nico_local/Demo/SalesForce/python_ws/", line 72, in describeGlobal
        response = self.binding.Receive(describeGlobalResponse.typecode)
      File "build/bdist.macosx-10.3-fat/egg/ZSI/", line 503, in Receive
      File "build/bdist.macosx-10.3-fat/egg/ZSI/", 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/, 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...

    • mikeyp

      mikeyp - 2008-02-14

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

    • Fab

      Fab - 2008-06-03


      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 = ""
                  req.Expires = dt           
                  req.Filter = "Filter"

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

      resp = enforcementServerPortType.Subscribe(req, req_header)


      The Subcribe method make a Send :





      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,



Log in to post a comment.