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

时间:2022-05-25 20:12:24

I am wondering if I could use simply use HTTP POST Requests in order to implement a SOAP API.

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

If so how should I format and treat the requests?

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

3 个解决方案

#1


8  

Yep, I have done this in certain cases where SOAPpy did not work with a given schema. This should get you started.

是的,我在某些情况下做到了这一点,其中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())

For the content string, I would use SoapUI to make the requests manually, and then mimic the XML.

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

#2


3  

Yes, see http://www.w3schools.com/SOAP/soap_httpbinding.asp

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

A SOAP method is an HTTP request/response that complies with the SOAP encoding rules.

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

HTTP + XML = SOAP

HTTP + XML = SOAP

A SOAP request could be an HTTP POST or an HTTP GET request.

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

You'll sometimes find other transport mechanisms used for quality of service reasons, for instance using a messaging framework.

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

#3


0  

I found a great example of this in the simple-salesforce package. The code is in login.py and pasted below

我在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))

#1


8  

Yep, I have done this in certain cases where SOAPpy did not work with a given schema. This should get you started.

是的,我在某些情况下做到了这一点,其中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())

For the content string, I would use SoapUI to make the requests manually, and then mimic the XML.

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

#2


3  

Yes, see http://www.w3schools.com/SOAP/soap_httpbinding.asp

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

A SOAP method is an HTTP request/response that complies with the SOAP encoding rules.

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

HTTP + XML = SOAP

HTTP + XML = SOAP

A SOAP request could be an HTTP POST or an HTTP GET request.

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

You'll sometimes find other transport mechanisms used for quality of service reasons, for instance using a messaging framework.

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

#3


0  

I found a great example of this in the simple-salesforce package. The code is in login.py and pasted below

我在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))