Python XML SOAP 请求返回 400,但 Postman 返回 200

时间:2021-04-06 01:01:39

标签: python-3.x xml soap python-requests

我正在尝试使用请求来查询 SOAP API,并且我能够让它在 Postman 中正常工作。 但是,当我尝试使用 Python 中的请求发出请求时,我不断收到 400 响应。

这是我目前的代码:

from requests import Session
from zeep import Client
from zeep.transports import Transport
from zeep.wsa import WsAddressingPlugin
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
import certifi
import http.client
from OpenSSL import crypto
from zeep.wsse.signature import Signature
import xmlsec
import pprint
from json import dumps


headers = {'content-type':'application/soap+xml','action':'http://Blah/IBlah/EntitySearch'}
body = """
xml = <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:hsd="http://Blah">
   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://Blah/IBlah/EntitySearch</wsa:Action><wsa:To>https://blahblahblah:120/HSD.ProxyService/Blah.svc?wsdl</wsa:To></soap:Header>
   <soap:Body>
      <hsd:EntitySearch>
         <!--Optional:-->
         <hsd:request>
            <!--Optional:-->
            <hsd:EntityId>100000</hsd:EntityId>
         </hsd:request>    
      </hsd:EntitySearch>
   </soap:Body>
</soap:Envelope>""".encode('utf-8')

r = requests.post('https://blahblahblah:120/Blah.ProxyService/Blah.svc?wsdl',cert=('C:\\Users\\Me\\Desktop\\llama_cert.pem', 'C:\\Users\\Me\\Desktop\\llama.key'),data=body,headers=headers)
print(r.content)

SOAP XML 在 Postman 中确实有效,所以我不确定我哪里出错并得到 400 响应。我从 POST 切换到 GET 并取出数据和标题,我能够从中得到 200 响应。 我也在 Zeep 中这样做过,但 Zeep 要求我输入两个请求字段,而我真的需要让我的程序与一个一起工作。

我对如何解决此问题的任何想法持开放态度。

0 个答案:

没有答案