【问题标题】:My SOAP post response is not returning 200 in Python, but it works in Postman我的 SOAP 发布响应在 Python 中未返回 200,但在 Postman 中有效
【发布时间】:2021-01-02 19:09:07
【问题描述】:

所以我在 Postman 中运行 SOAP 的 post 方法并收到 200,响应标头有 text/xml; charset=utf-8 用于内容类型。我在 Postman 中的标头(不包括默认值)是

客户 ID 700, SOAPAction 骨灰盒, 内容类型 text/xml

网址 = www.xyz.com.svc

用于在 Postman 中运行的 SOAP 原始 XML。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.xsd" xmlns:wsu="http://docs.oa.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-1234">
                <wsse:Username>E</wsse:Username>
                <wsse:Password Type="http://docs.o-username-token-profile-1.0#PasswordText">abcd</wsse:Password>
                <wsse:Nonce EncodingType="http://docs.o.security-1.0#Base64Binary">happy</wsse:Nonce>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
<ConfirmAppointment xmlns="urn:CompanyNameServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--string occurs:0,1-->
<PatID xsi:nil="false">A1</PatID>
<!--string occurs:0,1-->
<PIDType xsi:nil="false">B</PIDType>
<!--string occurs:0,1-->
<MyCID xsi:nil="false">1</MyCID>
<!--string occurs:0,1-->
<MIDType xsi:nil="false">External</MyIDType>
<!--string occurs:0,1-->
<AppCID xsi:nil="false">1</AppCID>
</ConfirmAppointment>
    </soapenv:Body>
</soapenv:Envelope>

我正在尝试使用 Python requests.post 运行它,这是我的代码。

soap_body = '''

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.xsd" xmlns:wsu="http://docs.oa.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-1234">
                <wsse:Username>E</wsse:Username>
                <wsse:Password Type="http://docs.o-username-token-profile-1.0#PasswordText">abcd</wsse:Password>
                <wsse:Nonce EncodingType="http://docs.o.security-1.0#Base64Binary">happy</wsse:Nonce>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
<ConfirmAppointment xmlns="urn:CompanyNameServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--string occurs:0,1-->
<PatID xsi:nil="false">A1</PatID>
<!--string occurs:0,1-->
<PIDType xsi:nil="false">B</PIDType>
<!--string occurs:0,1-->
<MyCID xsi:nil="false">1</MyCID>
<!--string occurs:0,1-->
<MIDType xsi:nil="false">External</MyIDType>
<!--string occurs:0,1-->
<AppCID xsi:nil="false">1</AppCID>
</ConfirmAppointment>
    </soapenv:Body>
</soapenv:Envelope>'''

headers = {
'ClientID': '700',
'SOAPAction': 'urn',
'Content-Type': 'text/xml'
}

response = requests.post(url=url, data=soap_body, headers=headers)
print(response)

我的输出:

如果我将&lt;?xml version="1.0" encoding="UTF-8"?&gt; 添加到正文的开头,我的输出:

我在这里做错了什么?

【问题讨论】:

标签: python-3.x soap postman


【解决方案1】:

这里有 2 个示例 SOAP 请求。看看它们是否适合你。

import requests

url = "https://httpbin.org/post"

headers = {"content-type" : "application/soap+xml"}
body = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="https://httpbin.org/post">
   <soapenv:Header/>
   <soapenv:Body/>
</soapenv:Envelope>
""".strip()

response = requests.post(url, data = body, headers = headers)
print(response.content.decode("utf-8") +'\n')

###########################################

url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso"

headers = {"content-type" : "application/soap+xml"}
body = """
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
      <ubiNum>500</ubiNum>
    </NumberToWords>
  </soap:Body>
</soap:Envelope>
""".strip()

response = requests.post(url, data = body, headers = headers)
print(response.content.decode("utf-8") +'\n')

【讨论】:

  • 是的,这些对我来说很好。我会做更多的挖掘。
  • 打错了,ClientID 应该是 Client-ID >。
  • 酷 - 我喜欢简单的修复 :)
猜你喜欢
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 2015-05-09
  • 2019-12-04
  • 1970-01-01
相关资源
最近更新 更多