我有一个简单的API,它有一个post端点,需要三个参数。当我通过python-fask codegen SwaggerUI测试它时,我得到一个错误,说没有足够的值来解包。server-stub是从swagger-hub自动生成的,预计可以工作:)如何让它工作?以下是详细信息:
来自swagger yaml文件:
paths:
/estimate:
post:
tags:
- client_requests
summary: submits a job by specifying a script file
operationId: e_post
requestBody:
content:
application_json: #WRONG should be application/json as I later discovered.
schema:
$ref: '#/components/schemas/EJob'
responses:
"201":
description: job_received
"400":
description: invalid_input
callbacks:
statusUpdate:
'{$request.body#/callback_url}':
post:
requestBody:
content:
application_json:
schema:
$ref: '#/components/schemas/EJobStatus'
required: true
responses:
"200":
description: Accepted by the server
x-openapi-router-controller: swagger_server.controllers.client_requests_controller
和yaml中的Ejob Schema:
schemas:
EJob:
required:
- callback_url
- id
- script_url
type: object
properties:
id:
type: integer
format: int64
example: 39
script_url:
type: string
description: A job for backend to execute
format: uri
example: file:///data/39_script.json
callback_url:
type: string
description: This URL is called to update status
format: uri
example: http:://192.168.100.30:50/update_status
来自Swagger UI的post请求:
curl -X 'POST' \
'http://localhost:8080/BLA/BLA/1.0.0/estimate' \
-H 'accept: */*' \
-H 'Content-Type: application_json' \
-d '{
"callback_url": "http://192.168.100.30:50/update_status",
"id": 39,
"script_url": "file:///data/39_script.json"
}'
我从swaggerhub python-flask codegen生成的代码中得到的错误:
[2023-04-26 17:21:43,069] ERROR in app: Exception on /BLA/BLA/1.0.0/estimate [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/decorator.py", line 68, in wrapper
response = function(request)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/uri_parsing.py", line 149, in wrapper
response = function(request)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/validation.py", line 148, in wrapper
if all_json(self.consumes):
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 165, in all_json
return all(is_json_mimetype(mimetype) for mimetype in mimetypes)
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 165, in <genexpr>
return all(is_json_mimetype(mimetype) for mimetype in mimetypes)
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 139, in is_json_mimetype
maintype, subtype = mimetype.split('/') # type: str, str
ValueError: not enough values to unpack (expected 2, got 1)
172.17.0.1 - - [26/Apr/2023 17:21:43] "POST /BLA/BLA/1.0.0/estimate HTTP/1.1" 500
1条答案
按热度按时间ev7lccsx1#
post requestBody内容类型应该是application/json而不是application_json。理想情况下,swaggerhub上的编辑器应该将此标记为错误。