首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解析包含webargs中文件和正常字段的多部分表单数据?

如何解析包含webargs中文件和正常字段的多部分表单数据?
EN

Stack Overflow用户
提问于 2020-04-28 12:53:51
回答 2查看 916关注 0票数 3

我需要使用webargs解析带有附加文件的多部分表单数据。此时此刻,我有了下一个模型:

代码语言:javascript
复制
RAW_ARGS = {
    'file': fields.Field(
        required=True,
        validate=lambda file: file.mimetype == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        location='files'),
    'currency': fields.Int(required=True, validate=validate.OneOf([
        ECurrency.EUR.value,
        ECurrency.RUB.value,
        ECurrency.USD.value
    ]), location='form')
}

class RawResource(Resource):

    @token_required
    @use_args(RAW_ARGS)
    def post(self, args):
        return '', 204

但是,根据请求,我得到了使用The request was well-formed but was unable to be followed due to semantic errors. HTTP状态代码的422错误。

以下是Chrome网络的请求副本:

代码语言:javascript
复制
Request URL: http://localhost:5000/api/v1/raw
Request Method: POST
Status Code: 422 UNPROCESSABLE ENTITY
Remote Address: 127.0.0.1:5000
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Origin: http://localhost:4200
Content-Length: 186
Content-Type: application/json
Date: Tue, 28 Apr 2020 12:41:08 GMT
Server: Werkzeug/1.0.1 Python/3.8.1
Vary: Origin
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer <token>
Connection: keep-alive
Content-Length: 405453
Content-Type: multipart/form-data
Host: localhost:5000
Origin: http://localhost:4200
Referer: http://localhost:4200/upload
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
------WebKitFormBoundary5KsexIuuVJnu3TnU
Content-Disposition: form-data; name="currency"

840
------WebKitFormBoundary5KsexIuuVJnu3TnU
Content-Disposition: form-data; name="file"; filename="test.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


------WebKitFormBoundary5KsexIuuVJnu3TnU--

解析多部分表单数据的正确方法是什么?

P.S.我的服务器是烧瓶应用。

EN

回答 2

Stack Overflow用户

发布于 2021-04-29 16:29:44

写两遍@use_args,传递两遍。我已经相应地编辑了您的代码

代码语言:javascript
复制
RAW_ARGS = {
    'file': fields.Field(
        required=True,
        validate=lambda file: file.mimetype == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        location='files'),
    'currency': fields.Int(required=True, validate=validate.OneOf([
        ECurrency.EUR.value,
        ECurrency.RUB.value,
        ECurrency.USD.value
    ]), location='form')
}
class RawResource(Resource):
    @token_required
    @use_args(RAW_ARGS['file'])
    @use_args(RAW_ARGS['currency'])
    def post(self, args_file, args_currency):
        return '', 204
票数 0
EN

Stack Overflow用户

发布于 2022-04-28 14:31:54

您需要的是用于formfiles位置(即form_and_files)的自定义数据加载器。我刚刚在这个公关中实现了它

代码语言:javascript
复制
from webargs.multidictproxy import MultiDictProxy


@parser.location_loader('form_and_files')
def load_form_and_files(request, schema):
    form_and_files_data = request.files.copy()
    form_and_files_data.update(request.form)
    return MultiDictProxy(form_and_files_data, schema)

然后,您可以在视图的form_and_files装饰器中声明位置use_args

代码语言:javascript
复制
@use_args(RAW_ARGS, location='form_and_files_data')

请参阅理发师中的详细信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61480667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档