FlaskRestPlus RESTAPI - 如何将文件附件和有效负载作为参数?

问题描述

我在使用 Flask RestPlus 时遇到了一些问题。我可以在 POST 请求中单独应用 json 有效负载文件上传没问题,但是一起,它似乎不起作用。有人可以请告知问题是什么吗?

我得到的错误是:

{
    "errors": {
        "": "None is not of type 'object'"
    },"message": "Input payload validation Failed"
}

这是我的代码示例:

class LdamodelDto:
    api = Namespace('Ldamodel',description='apply topic modeling')
    topic_modeling = api.model('topic_modeling',{
        'column_name': fields.String(required=True,description='name of column that you want to analyze'),'number_clusters': fields.Integer(required=True,description='number of categorizations you expect')
    })

api = LdamodelDto.api
_topic_modeling = LdamodelDto.topic_modeling

file_upload = reqparse.RequestParser()
file_upload.add_argument('file',type=FileStorage,location='files',required=True,help='data file to be uploaded'
                         )

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.',1)[1].lower() in allowed_file_types

@api.route('/topic_modeling')
class TopicModeling(Resource):

    @api.expect(_topic_modeling,file_upload,validate=True)
    @api.doc('return CSV file containing LDA model output and HTML for LDA visualization')
    def post(self):
        """run LDA model on text data and return attachments files"""
        for k,v in request.json.items():
             if not v:
                abort(400,f"{k} cannot be null")

        args = file_upload.parse_args()
        file = args['file']

        if file.filename == '':
            abort(400,'No file selected for uploading')

        if allowed_file(file.filename):
            filename = secure_filename(file.filename)
            print(pd.read_csv(file))
            return filename

        else:
            return abort(400,'Allowed file types are %s' % ','.join(allowed_file_types))

以下是我尝试执行 api 调用的示例:

payload = {
    'column_name': 'test','number_clusters': 5
}

r = requests.post('http://127.0.0.1:5000/Ldamodel/topic_modeling',json=payload,files={
                      'file': open('../topics_dataset.csv')}
                  )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...