“The SOAP request must use SOAP 1.1…”

℡╲_俬逩灬. 提交于 2019-12-12 03:16:53

问题


I am writing some code that generates XML and, using the requests library, POSTs the XML to Salesforce.com's SOAP service. Here is the code that I'm using to generate the XML:

from lxml import etree

class SalesforceLeadConverter(object):

    def __init__(self, session_id, lead_id, **kwargs):

        self.session_id = session_id
        self.lead_id = lead_id

    def build_xml(self):
        root = etree.Element(
            '{soapenv}Envelope',
            soapenv='<a rel="nofollow" class="external free" href="http://schemas.xmlsoap.org/soap/envelope/">http://schemas.xmlsoap.org/soap/envelope/</a>',
            urn='enterprise.soap.sforce.com'
            )
        soapenv = etree.SubElement(root, '{soapenv}Header')
        urn = etree.SubElement(soapenv, '{urn}SessionHeader')
        session_id = etree.SubElement(urn, '{urn}sessionId').text=self.session_id
        soapenv2 = etree.SubElement(root, '{soapenv}Body')
        urn2 = etree.SubElement(soapenv2, '{urn}convertLead')
        lead_converts = etree.SubElement(urn2, '{urn}leadConverts')
        lead_id = etree.SubElement(lead_converts, '{urn}leadId').text=self.lead_id

        return """<?xml version="1.1" encoding="utf-8"?>""" + etree.tostring(root)

Then from the shell, I try to make a request as follows:

 >>> from myapp.salesforce.soap import SalesforceLeadConverter
 >>> slc = SalesforceLeadConverter(session_id="foo...", lead_id="00Qxkdf...")
 >>> xml = slc.build_xml()
 >>> import requests
 >>> headers = {'Content-Type':'text/xml', 'SOAPAction':'convertLead'}
 >>> out = requests.post('https://na1.salesforce.com/services/Soap/c/10.0', data=xml, headers=headers)

out returns a 500 error, and the error text returns

soapenv:VersionMismatchThe SOAP request must use SOAP 1.1, did not recieve a SOAP 1.1 Envelope as the document root'

I inspected the xml value, which is:

'<?xml version="1.0" encoding="utf-8"?><ns0:Envelope xmlns:ns0="soapenv" soapenv="&lt;a rel=&quot;nofollow&quot; class=&quot;external free&quot; href=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;http://schemas.xmlsoap.org/soap/envelope/&lt;/a&gt;" urn="enterprise.soap.sforce.com"><ns0:Header><ns1:SessionHeader xmlns:ns1="urn"><ns1:sessionId>00Dj0000001obaf!AR8AQAvJgjiwkh.BbJa48PB2RZ_ITHBqoZCDa2tf98_6kir5xGBSX4Iz_E7Mt3i_.RwylUoNNEHrrKsyR9huvrxIYoCdUg95</ns1:sessionId></ns1:SessionHeader></ns0:Header><ns0:Body><ns2:convertLead xmlns:ns2="urn"><ns2:leadConverts><ns2:leadId>00Qj000000PMV3h</ns2:leadId><ns2:doNotCreateOpportunity>False</ns2:doNotCreateOpportunity><ns2:sendNotificationEmail>False</ns2:sendNotificationEmail></ns2:leadConverts></ns2:convertLead></ns0:Body></ns0:Envelope>'

I'm wondering if the HTML encoding set to soapenv in the header envelope is the issue, and if so, how do I prevent that encoding? Is there something else that I'm doing wrong?


回答1:


After a lengthy research process, I came across this post, which recommended the following:

from lxml import etree

S_NS = 'http://schemas.xmlsoap.org/soap/envelope/'
S_PRE = '{' + S_NS + '}'

env = etree.Element(S_PRE + 'Envelope', nsmap={'soapenv': S_NS})
header = etree.SubElement(env, S_PRE + 'Header')
body = etree.SubElement(env, S_PRE + 'Body')

print(etree.tostring(env, pretty_print=True, encoding='unicode'))


来源:https://stackoverflow.com/questions/39183423/the-soap-request-must-use-soap-1-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!