Search code examples
pythonpython-3.xsoapspyne

Can't figure out how to return a Python dict/Json as xml using Spyne


I have fired up a WSGI application using Spyne for some SOAP services that I'm trying to build.

I'm absolutely new to SOAP and Spyne in general and I can't seem to figure out how to return a JSON/Python dict as XML. This is what I've done.

class Fruits(ServiceBase):

@rpc(_returns=Iterable(Unicode))
def fruitify(self):
    fruits  = {"apple" : "1", "orange" : ["2","3","4"]}
    return fruits

I think the problem lies in the decorator I'm specifying using _returns.

I tried reading the docs again and again but couldn't figure it out.

The response I'm getting is something like:

    <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="lets_fruit">
   <soap11env:Body>
      <tns:fruitifyResponse>
         <tns:fruitifyResult>
            <tns:string>apple</tns:string>
            <tns:string>orange</tns:string>
         </tns:fruitifyResult>
      </tns:fruitifyResponse>
   </soap11env:Body>
</soap11env:Envelope>

As evident, it does not have any of my values associated to keys.

Has anybody done something similar and successfully implemented this before?

Thanks in advance!


Solution

  • Figured out guys.

    I just had to change my _returns=Iterable(Unicode) to _returns=AnyDict.

    Thanks!