"SOAP请求必须使用SOAP 1.1 ..." [英] "The SOAP request must use SOAP 1.1..."
问题描述
我正在编写一些生成XML的代码,并使用requests
库将XML POST到Salesforce.com的SOAP服务.这是我用来生成XML的代码:
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
返回500错误,错误文本返回
out
returns a 500 error, and the error text returns
soapenv:VersionMismatchThe SOAP请求必须使用SOAP 1.1,未将SOAP 1.1 Envelope接收为 文件 根'
soapenv:VersionMismatchThe SOAP request must use SOAP 1.1, did not recieve a SOAP 1.1 Envelope as the document root'
我检查了xml
值,即:
'<?xml version="1.0" encoding="utf-8"?><ns0:Envelope xmlns:ns0="soapenv" 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"><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>'
我想知道在标头信封中设置为soapenv的HTML编码是否是问题,如果是这样,如何防止该编码?还有我在做错什么吗?
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?
推荐答案
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'))
这篇关于"SOAP请求必须使用SOAP 1.1 ..."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!