1

嗨,我正在使用zeep使用基于肥皂的 Web 服务,并且我不断收到 HTTP 状态 415 错误。我挖了一下,使用了Pycharm Debuggger,发现原因是:

'无法处理消息,因为内容类型\'text/xml; charset=utf-8 XaSOfalw: rtt; ___utmvmBfuwVEwB=yEnqIuCmRhw\' 不是预期的类型 \'text/xml; 字符集=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(无可用内容)

4

1 回答 1

1

我已经通过在 zeep 客户端中使用插件解决了这个问题。

我的代码如下所示:

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;它出奇地有效。

于 2017-12-21T01:52:46.973 回答