我正在尝试上传一个文件,并使用 Swagger UI 以 json 格式从用户那里获取输入。我已经为此编写了以下代码。
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
Run Code Online (Sandbox Code Playgroud)
当我单独运行上述内容时,例如,如果我只上传文件或只从用户那里获取输入,则代码有效,但如果我一起执行。tax_from 返回None值,它不会将我通过 Swagger UI 输入的值作为 json 值。