我可以使用SOAP的HTTP Post请求吗? - SOAP和Django

时间:2010-04-02 17:40:12

标签: python django http soap post

我想知道我是否可以使用HTTP POST请求来实现SOAP API。

如果是这样,我应该如何格式化和处理请求?

3 个答案:

答案 0 :(得分:8)

是的,我在某些情况下已经完成了这项工作,其中SOAPpy不适用于给定的架构。这应该可以帮到你。

import httplib
from xml.dom import minidom

http.request("POST", "/path/to/my/webservice", body=xml, headers = {
    "Host": "myservername",
    "Content-Type": "text/xml; charset=UTF-8",
    "Content-Length": len(xml)
})

print minidom.parseString(http.getresponse().read())

对于内容字符串,我会使用SoapUI手动发出请求,然后模仿XML。

答案 1 :(得分:3)

是的,请参阅http://www.w3schools.com/SOAP/soap_httpbinding.asp

  

SOAP方法是HTTP   请求/响应符合   SOAP编码规则。

     

HTTP + XML = SOAP

     

SOAP请求可以是HTTP POST   或HTTP GET请求。

您有时会发现其他传输机制用于服务质量的原因,例如使用消息传递框架。

答案 2 :(得分:0)

我在simple-salesforce包中找到了一个很好的例子。代码位于login.py并粘贴在

下方
soap_url = 'https://{domain}.salesforce.com/services/Soap/u/{sf_version}'
domain = 'test' if sandbox else 'login'

soap_url = soap_url.format(domain=domain, sf_version=sf_version)

username = escape(username)
password = escape(password)

# Check if token authentication is used
if 'security_token' in kwargs:
    security_token = kwargs['security_token']

    # Security Token Soap request body
    login_soap_request_body = """<?xml version="1.0" encoding="utf-8" ?>
    <env:Envelope
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
        <env:Body>
            <n1:login xmlns:n1="urn:partner.soap.sforce.com">
                <n1:username>{username}</n1:username>
                <n1:password>{password}{token}</n1:password>
            </n1:login>
        </env:Body>
    </env:Envelope>""".format(username=username, password=password, token=security_token)

# Check if IP Filtering is used in cojuction with organizationId
elif 'organizationId' in kwargs:
    organizationId = kwargs['organizationId']

    # IP Filtering Login Soap request body
    login_soap_request_body = """<?xml version="1.0" encoding="utf-8" ?>
    <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:urn="urn:partner.soap.sforce.com">
        <soapenv:Header>
            <urn:CallOptions>
                <urn:client>RestForce</urn:client>
                <urn:defaultNamespace>sf</urn:defaultNamespace>
            </urn:CallOptions>
            <urn:LoginScopeHeader>
                <urn:organizationId>{organizationId}</urn:organizationId>
            </urn:LoginScopeHeader>
        </soapenv:Header>
        <soapenv:Body>
            <urn:login>
                <urn:username>{username}</urn:username>
                <urn:password>{password}</urn:password>
            </urn:login>
        </soapenv:Body>
    </soapenv:Envelope>""".format(
        username=username, password=password, organizationId=organizationId)

else:
    except_code = 'INVALID AUTH'
    except_msg = 'You must submit either a security token or organizationId for authentication'
    raise SalesforceAuthenticationFailed(except_code, except_msg)

login_soap_request_headers = {
    'content-type': 'text/xml',
    'charset': 'UTF-8',
    'SOAPAction': 'login'
}
response = requests.post(soap_url,
                         login_soap_request_body,
                         headers=login_soap_request_headers,
                         proxies=kwargs.get('proxies', None))