使用Python Connexion开发REST服务时遇参数缺失错误排查
问题:Connexion POST创建Applicant接口提示缺少参数错误
错误栈信息
Traceback (most recent call last): File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 2548, in __call__ return self.wsgi_app(environ, start_response) File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 2528, in wsgi_app response = self.handle_exception(e) File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 2525, in wsgi_app response = self.full_dispatch_request() File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 1822, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 1820, in full_dispatch_request rv = self.dispatch_request() File "/opt/homebrew/lib/python3.9/site-packages/flask/app.py", line 1796, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "/opt/homebrew/lib/python3.9/site-packages/connexion/decorators/decorator.py", line 68, in wrapper response = function(request) File "/opt/homebrew/lib/python3.9/site-packages/connexion/decorators/uri_parsing.py", line 149, in wrapper response = function(request) File "/opt/homebrew/lib/python3.9/site-packages/connexion/decorators/validation.py", line 196, in wrapper response = function(request) File "/opt/homebrew/lib/python3.9/site-packages/connexion/decorators/produces.py", line 41, in wrapper response = function(request) File "/opt/homebrew/lib/python3.9/site-packages/connexion/decorators/parameter.py", line 120, in wrapper return function(**kwargs) TypeError: create() missing 1 required positional argument: 'applicant'
接口调用curl命令
curl -X 'POST' \ 'http://localhost:8181/myco/app-orchestrator/1.0.0/applicant' \ -H 'accept: text/plain' \ -H 'Content-Type: application/json' \ -d '{ "addressLine1": "123 Street", "addressLine2": "unit 2309", "birthDate": "01/01/2021", "city": "San Francisco", "emailAddress": "test@test.com", "firstName": "Test2", "lastName": "Tester2", "middleName": "T", "phoneNumber": "123-321-4321", "ssn": "123456789", "state": "CA", "zip": "12345-1234" }'
问题背景与排查方向
使用Connexion、Marshmallow和SqlAlchemy开发Python基础REST服务,调用创建applicant的POST接口时出现上述错误。
最初怀疑需要在POST请求中传入自增主键applicantId(字段定义为db.Column(db.Integer, primary_key=True)),但随后认为更可能是Swagger规范配置问题,导致请求体未传递给applicants.create方法。
示例代码已上传至GitHub仓库。
复现步骤
- 执行
python build_database.py - 执行
python server.py
Swagger UI地址:http://localhost:8181/myco/app-orchestrator/1.0.0/ui
相关Swagger配置片段
/applicant: post: summary: Create applicant operationId: applicants.create requestBody: content: application/json: schema: $ref: '#/components/schemas/Applicant' required: true responses: '201': description: Applicant created content: text/plain: schema: type: string example: Applicant created '400': description: invalid request '500': description: error
内容的提问来源于stack exchange,提问作者lightweight
相关产品推荐
相关产品推荐

