'How to post xml data to soap service and process properly via spyne Python?
I want to get xml body:
<root>
<row>
<param1>value1</param1>
<param2>value2</param2>
</row>
<row>
<param1>value3</param1>
<param2>value4</param2>
</row>
</root>
in param input_data in xml form. My code is:
from spyne import Application, rpc, ServiceBase, Unicode
from lxml import etree
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
class TestService(ServiceBase):
@rpc(Unicode, _returns=Unicode)
def load_data(ctx, input_data):
print(etree.tostring(ctx.in_document))
print('\n')
print(input_data)
app = Application([TestService], tns='PostData',
in_protocol=Soap11(),
out_protocol=Soap11(),
)
application = WsgiApplication(app)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
server = make_server('0.0.0.0', 8090, application)
server.serve_forever()
raw XML in Postman:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tran="PostData">
<soapenv:Header/>
<soapenv:Body>
<tran:load_data>
<tran:input_data>
<root>
<row>
<param1>value1</param1>
<param2>value2</param2>
</row>
<row>
<param1>value3</param1>
<param2>value4</param2>
</row>
</root>
</tran:input_data>
</tran:load_data>
</soapenv:Body>
</soapenv:Envelope>
With current implementation it doesn't work, param input_data is not shown (print(input_data)). Maybe need change param in @rpc? There can be many raw bodies. (Next step is to translate xml to json and load into table and I need data in xml format) How can I do it? Thanks
Solution 1:[1]
First, read the manual. Here's the relevant chapter: http://spyne.io/docs/2.10/manual/03_types.html
There are also a bunch of examples in the spyne repository: https://github.com/arskom/spyne/tree/master/examples
class Row(ComplexModel):
param1 = Unicode
param2 = Unicode
class Root(ComplexModel):
row = Row
class TestService(ServiceBase):
@rpc(Root, _returns=Unicode)
def load_data(ctx, input_data):
print(etree.tostring(ctx.in_document))
print('\n')
print(input_data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Burak Arslan |
