Python Zeep - HTTP状态415(无内容可用)

问题描述:

您好我正在使用zeep消耗基于soap的网络服务, ,我一直在获取HTTP状态415错误。我向下挖了一下,使用 Pycharm Debuggger,发现原因是:Python Zeep - HTTP状态415(无内容可用)

'Cannot process the message because the content type \'text/xml; charset=utf-8 XaSOfalw: rtt; ___utmvmBfuwVEwB=yEnqIuCmRhw\' was not the expected type \'text/xml; charset=utf-8\'.'

什么是错的内容类型?以及如何在Zeep中更改它?

我刚刚创建了一个简单的测试代码,看起来像这样:

from zeep import Client 

pretend_wsdl = 'https://pretendwsdl' 
client = Client(wsdl=pretend_wsdl) 

res = client.service.NameOfService() 
print(res) 

,并得到这个错误:在ZEEP客户使用plugins

zeep.exceptions.TransportError: Server returned HTTP status 415 (no content available)

我已经解决了这个问题。

我的代码如下所示:

from zeep import Client 
from zeep import Plugin 


class MyLoggingPlugin(Plugin): 

    def ingress(self, envelope, http_headers, operation): 
     return envelope, http_headers 

    def egress(self, envelope, http_headers, operation, binding_options): 
     http_headers['Content-Type'] = 'text/xml; charset=utf-8;' 
     return envelope, http_headers 


pretend_wsdl = 'https://pretendwsdl.com' 

client = Client(wsdl=pretend_wsdl, plugins=[MyLoggingPlugin()]) 

res = client.service.NameOfService() 

print(res) 

我觉得很奇怪,因为ZEEP的默认内容类型为text/xml;字符集= UTF-8; 和我使用的wsdl不认为来自zeep的内容类型是text/xml;字符集= UTF-8;

所以我用zeep插件明确设置内容类型为text/xml;字符集= UTF-8;它令人惊讶地工作。