[SalesForce] parsing outbound message in python

I am capturing outbound message from salesforce as a POST

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
    <OrganizationId>00D280000011nfmEAA</OrganizationId>
    <ActionId>04k280000008RaNAAU</ActionId>
    <SessionId>00D280000011nfm!AQkAQP0UUCMjx45Y8JUjboFldPYjgUE_GsERgn6zkrhzIqiq4bnabtPWRo..eOE7q_pVQREdkKMi3cHtem5OoW4swVbYX1c1</SessionId>
    <EnterpriseUrl>https://ap2.salesforce.com/services/Soap/c/34.0/00D280000011nfm</EnterpriseUrl>
    <PartnerUrl>https://ap2.salesforce.com/services/Soap/u/34.0/00D280000011nfm</PartnerUrl>
    <Notification>
      <Id>04l28000000HkGRAA0</Id>
      <sObject xsi:type="sf:Account" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
        <sf:Id>001280000077HXLAA2</sf:Id>
        <sf:Name>University Of Hyderabad</sf:Name>
        <sf:Phone>(898) 549-5538</sf:Phone>
      </sObject>
    </Notification>
   </notifications>
 </soapenv:Body>
</soapenv:Envelope>  

and want to parse it using python, but getting an error

XML or text declaration not at start of entity: line 2, column 0

My code sample is

from twisted.web import server, resource
from twisted.internet import reactor
import os
import json
import xml.etree.ElementTree as ET

class Simple(resource.Resource):
    isLeaf = True

    def render_GET(self, request):
        return "{0}".format(request.args.keys())

    def render_POST(self, request):
        #print request.getHeader('User-Agent')
        msg = request.content.read()
        #print msg
        root = ET.fromstring(msg)
        print root.tag
        for child in root:
            print(child.tag, child.attrib)
        return xmlRes

site = server.Site(Simple())
reactor.listenTCP(7080, site)
reactor.run()

Any help will be appreciable.

Best Answer

The first line must have a new line in place for that error to appear, try checking the XML you are sending, the MUST BE the first characters to be sent

Related Topic