基于WSDL的简易计算器Postman请求失败问题求助
如何用Postman调用基于Spyne的SOAP计算器服务?
我是Postman和Web服务的新手,正尝试通过Postman结合WSDL实现一个简易计算器Web服务,但不清楚如何通过Postman的Body或Header发送请求。已尝试配置Header参数,但仍无法使服务正常运行,请求排查原因及解决方法。
我的Python服务代码:
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()
排查及解决方法:
1. 先确认服务正常运行
运行上述Python代码后,访问http://127.0.0.1:8000/?wsdl,能看到完整的WSDL文档,说明服务启动成功。
2. Postman核心配置步骤
SOAP服务对请求格式有严格要求,以下是正确配置:
- 请求方法与URL:选择
POST,URL填写http://127.0.0.1:8000/ - Header配置:必须添加
Content-Type: text/xml; charset=utf-8,这是SOAP请求的必填头,也是最容易出错的点 - Body配置:选择
raw格式,下拉框选XML,然后按照WSDL定义的结构编写请求体
3. 示例请求体(以addition方法为例)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap"> <soapenv:Header/> <soapenv:Body> <ser:addition> <ser:a>5</ser:a> <ser:b>3</ser:b> </ser:addition> </soapenv:Body> </soapenv:Envelope>
4. 常见错误排查
- 检查
Content-Type是否正确:不要使用application/json或其他类型,必须是text/xml - 确认XML命名空间:
xmlns:ser="services.calculator.soap"必须和代码中Application的第二个参数一致 - 参数标签匹配:方法名(如
addition)、参数名(如a、b)要和WSDL定义的完全一致,注意大小写 - 端口占用:确保8000端口未被其他程序占用,可通过修改代码中的端口号测试
5. 其他方法请求示例
减法(substraction):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap"> <soapenv:Header/> <soapenv:Body> <ser:substraction> <ser:a>10</ser:a> <ser:b>4</ser:b> </ser:substraction> </soapenv:Body> </soapenv:Envelope>
乘法(multiplication):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap"> <soapenv:Header/> <soapenv:Body> <ser:multiplication> <ser:a>6</ser:a> <ser:b>7</ser:b> </ser:multiplication> </soapenv:Body> </soapenv:Envelope>
除法(division):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="services.calculator.soap"> <soapenv:Header/> <soapenv:Body> <ser:division> <ser:a>12</ser:a> <ser:b>3</ser:b> </ser:division> </soapenv:Body> </soapenv:Envelope>
内容的提问来源于stack exchange,提问作者AlexandrescuPaul
相关产品推荐
相关产品推荐

