【问题标题】:Python Zeep - HTTP status 415 (no content available)Python Zeep - HTTP 状态 415(无可用内容)
【发布时间】:2018-05-31 17:57:59
【问题描述】:
您好,我正在使用zeep 使用基于肥皂的网络服务,
我不断收到 HTTP 状态 415 错误。我挖了一点并使用
Pycharm Debugger,发现原因是:
'无法处理消息,因为内容类型\'text/xml;
charset=utf-8 XaSOfalw: rtt; ___utmvmBfuwVEwB=yEnqIuCmRhw\' 是
不是预期的类型 \'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.exceptions.TransportError:服务器返回 HTTP 状态 415(否
内容)
【问题讨论】:
标签:
python
web-services
soap
zeep
【解决方案1】:
我已经通过在 zeep 客户端中使用 plugins 解决了这个问题。
我的代码如下所示:
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;而且效果惊人。