[英]swagger flask restplus, upload a file and take json input together
I am trying to upload a file and well as take an input from the user in json format using Swagger UI.我正在尝试上传一个文件,并使用 Swagger UI 以 json 格式从用户那里获取输入。 I have written the below code for the same.
我已经为此编写了以下代码。
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
type = api.model("tax", {
"tax_form": fields.String()})
@api.route('/extraction')
@api.expect(upload_parser)
class extraction(Resource):
@api.expect(type)
def post(self):
tax_form= api.payload # json input string
print(tax_form['tax_form'])
args = upload_parser.parse_args() # upload a file
uploaded_file = args['file']
output = func_extract(uploaded_file,tax_form['tax_form'])
return output, 201
When i run the above individually for eg, if i only upload a file or only take an input from user, the code works but if i do them together.当我单独运行上述内容时,例如,如果我只上传文件或只从用户那里获取输入,则代码有效,但如果我一起执行。 tax_from returns None value, it does not take what I am inputting as json value via Swagger UI.
tax_from 返回None值,它不会将我通过 Swagger UI 输入的值作为 json 值。
I got the issue solved.我解决了这个问题。 Used reqparse for inputting the argument.
使用 reqparse 输入参数。 See the code snippet as below
请参阅下面的代码片段
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
parser = reqparse.RequestParser()
parser.add_argument('tax_form', required = True)
@api.route('/extraction')
@api.expect(upload_parser)
class extraction(Resource):
@api.expect(parser)
def post(self):
"""
extract the content
"""
args1 = parser.parse_args()
tax_form = args1['tax_form']
print(tax_form)
args = upload_parser.parse_args()
uploaded_file = args['file']
output = func_extract(uploaded_file,tax_form)
return output, 201
I recommend using list of models and parsers in api.expect with validate=True(if required).我建议使用 api.expect 中的模型和解析器列表与 validate=True(如果需要)。 This will remove the dependency of defining the expected query parameter (in your case) in the class level as you may have GET/PUT/DELETE API on the same route which may not even need this parameter.
这将消除在类级别定义预期查询参数(在您的情况下)的依赖性,因为您可能在同一路由上具有 GET/PUT/DELETE API,甚至可能不需要此参数。
Have modified your code to understand better:已修改您的代码以更好地理解:
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
tax_type = api.model("tax", {"tax_form": fields.String()})
@api.route('/extraction')
class extraction(Resource):
@api.expect(tax_type, upload_parser, validate=True)
def post(self):
tax_form= api.payload # json input string
args = upload_parser.parse_args() # upload a file
uploaded_file = args['file']
output = func_extract(uploaded_file,tax_form['tax_form'])
return output, 201
# This METHOD is now independent of your POST data expectations
def get(self):
output = {} # Some JSON
return output, 200
Also, please avoid using python reserved keywords like 'type' as variables.另外,请避免使用 python 保留关键字,如 'type' 作为变量。
Hope this helps ..!!希望这可以帮助 ..!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.