Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 是否有一种方法可以使用flask读取表单数据和上载的文件(多部分/表单数据)_Rest_Http_Flask - Fatal编程技术网

Rest 是否有一种方法可以使用flask读取表单数据和上载的文件(多部分/表单数据)

Rest 是否有一种方法可以使用flask读取表单数据和上载的文件(多部分/表单数据),rest,http,flask,Rest,Http,Flask,我公开了一个包含content type=multipart/form数据的FlaskAPI,该数据以json和.zip文件作为输入。当我尝试阅读时,我能够得到zip文件。但是,我无法读取json文件。我尝试了几种方法,但都没有json数据。这是否得到支持 我使用flask restplus解析器使用“werkzeug.datastructures import FileStorage”读取文件内容,而为了获取json,我使用了下面给出的不同选项,但都不起作用 ->申请表 ->dict(请求表)

我公开了一个包含content type=multipart/form数据的FlaskAPI,该数据以json和.zip文件作为输入。当我尝试阅读时,我能够得到zip文件。但是,我无法读取json文件。我尝试了几种方法,但都没有json数据。这是否得到支持

我使用flask restplus解析器使用“werkzeug.datastructures import FileStorage”读取文件内容,而为了获取json,我使用了下面给出的不同选项,但都不起作用 ->申请表

->dict(请求表)

->request.files.get_json(force=True)

->api.payload

from werkzeug.datastructures import FileStorage, ImmutableMultiDict


parser= api.parser()
parser.add_argument('opc-retry-token', location='headers',required='true')

parser.add_argument('file', location='files', type=FileStorage, required=True)

def init(self):
        args = parser.parse_args()
        token = args['opc-retry-token']
        self.token=token
        self.args=args
        logger.info(args) 

# above log  gives {'opc-retry-token': '1234', 'file': <FileStorage:         


@ns.route('/prepare')
@api.expect(parser)
class OICPrepare(Resource):

    @api.expect(oic_prepare)
    def post(self):

        init(self)
        logger.info(request.form)
# above log gives 'None'
        data = dict(request.form)
        logger.info(data)
# above log gives ImmutableMultiDict([])

        logger.info(request.files.get_json(force=True))
#above log gives {}       
        upload_file = self.args['file']
        upload_file.save('/oic_admin/wallet.zip')
#above save command does the zip file properly 
从werkzeug.datastructures导入文件存储,ImmutableMultiDict
parser=api.parser()
parser.add_参数('opc-retry-token',location='headers',required='true'))
add_参数('file',location='files',type=FileStorage,required=True)
def初始化(自):
args=parser.parse_args()
token=args['opc-retry-token']
self.token=token
self.args=args
logger.info(args)

#上面的日志给出了{'opc-retry-token':'1234','file':据我所知,Flask RESTPlus一次只支持上传一个文件。不过,纯Flask解决方案支持多个文件,请参见。json是以formdata的形式提交的。如{“data”:{“a”:“string”},{“b”:“string”}