Im new to postman and web services and i am trying to implement a simple calculator web service using postman and a wsdl.The problem is i dont know how to send request via body or header of postman.
My python code is: from spyne import Application, rpc, ServiceBase, Integer, Double from spyne.protocol.soap import Soap11 from spyne.server.wsgi import WsgiApplication
class CalculatorService(ServiceBase):
@rpc(Integer, Integer, _returns=Integer)
def addition(ctx, a, b):
return a b
@rpc(Integer, Integer, _returns=Integer)
def substraction(ctx, a, b):
return a - b
@rpc(Integer, Integer, _returns=Integer)
def multiplication(ctx, a, b):
return a * b
@rpc(Integer, Integer, _returns=Double)
def division(ctx, a, b):
return a / b
application = Application([CalculatorService], 'services.calculator.soap',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.INFO)
logging.getLogger('spyne.protocol.xml').setLevel(logging.INFO)
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://127.0.0.1:8000/?wsdl")
server = make_server('127.0.0.1', 8000, wsgi_application)
server.serve_forever()
Tried completing the header parametres but cant seem to get why it isnt functioning.
CodePudding user response:
You missed a input parameters <a> and <b> and name service services.calculator.soap
.
This is addition input
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap">
<soapenv:Header/>
<soapenv:Body>
<ser:addition>
<!--Optional:-->
<ser:a>6</ser:a>
<!--Optional:-->
<ser:b>9</ser:b>
</ser:addition>
</soapenv:Body>
</soapenv:Envelope>
SoapUI make life easier. Here is steps
#1 Python code save as simple-calculator.py
from spyne import Application, rpc, ServiceBase, Integer, Double
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
class CalculatorService(ServiceBase):
@rpc(Integer, Integer, _returns=Integer)
def addition(ctx, a, b):
return a b
@rpc(Integer, Integer, _returns=Integer)
def substraction(ctx, a, b):
return a - b
@rpc(Integer, Integer, _returns=Integer)
def multiplication(ctx, a, b):
return a * b
@rpc(Integer, Integer, _returns=Double)
def division(ctx, a, b):
return a / b
application = Application([CalculatorService], 'services.calculator.soap',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.INFO)
logging.getLogger('spyne.protocol.xml').setLevel(logging.INFO)
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://127.0.0.1:8000/?wsdl")
server = make_server('127.0.0.1', 8000, wsgi_application)
server.serve_forever()
#2 Install & Run it
pip install spyne
python simple-calculator.py
#3 Check WSDL is working by browser.
http://localhost:8000/?wsdl
#4 Create SOAP project by SoapUI
#5 Create SOAP input XML by clicking Request 1
of addition
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap">
<soapenv:Header/>
<soapenv:Body>
<ser:addition>
<!--Optional:-->
<ser:a>?</ser:a>
<!--Optional:-->
<ser:b>?</ser:b>
</ser:addition>
</soapenv:Body>
</soapenv:Envelope>
#6 Run it after enter input of <a> and <b>, then press green run button
The result will return in right side of panel. If you copy/pasted it (right side a red box) into VS Code and format document. It will be more readable pretty format.
Before
<?xml version='1.0' encoding='UTF-8'?>
<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="services.calculator.soap"><soap11env:Body><tns:additionResponse><tns:additionResult>13</tns:additionResult></tns:additionResponse></soap11env:Body></soap11env:Envelope>
After
<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="services.calculator.soap">
<soap11env:Body>
<tns:additionResponse>
<tns:additionResult>15</tns:additionResult>
</tns:additionResponse>
</soap11env:Body>
</soap11env:Envelope>
Same steps for division
, multiplication
and subtraction
The division
in Postman