Adding xsi:type and envelope namespace when using SUDS

笑着哭i 提交于 2019-12-01 01:47:39

The solution I found was to use a MessagePlugin to essentially manually fix up the XML just before sending. I'd hoped there was something more elegant, but at least this works:

class SoapFixer(MessagePlugin):

    def marshalled(self, context):
        # Alter the envelope so that the xsd namespace is allowed
        context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'
        # Go through every node in the document and apply the fix function to patch up incompatible XML. 
        context.envelope.walk(self.fix_any_type_string)

    def fix_any_type_string(self, element):
        """Used as a filter function with walk in order to fix errors.
        If the element has a certain name, give it a xsi:type=xsd:string. Note that the nsprefix xsd must also
         be added in to make this work."""
        # Fix elements which have these names
        fix_names = ['elementnametofix', 'anotherelementname']
        if element.name in fix_names:
            element.attributes.append(Attribute('xsi:type', 'xsd:string'))

It's sad and hilarious, like a lot of things about this particular library, but here's the exact answer:

http://lists.fedoraproject.org/pipermail/suds/2011-September/001519.html

from the above:

soapenv = soapenv.encode('utf-8')
plugins.message.sending(envelope=soapenv)

becomes:

soapenv = soapenv.encode('utf-8')
ctx = plugins.message.sending(envelope=soapenv)
soapenv = ctx.envelope

basically, it's a bug in the implementation, and you can patch it yourself by editing the line that runs the plugin to actually return the plugin's results, but I'm not aware of a patched and updated version of SUDS that fixes this yet (tho I haven't looked closely for it).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!