Flask - request.files 在 before_request 修饰函数中变为空字典

问题描述 投票:0回答:0

不知道有没有人遇到过这个问题。我的 Flask 在运行时表现异常。所以,给定以下代码:

@bb.before_request
def check_if_auth():
    # Deals with CORS ISSUE FOR Preflight Requests
    ## This needs to be the first line.
    if request.method == "OPTIONS":
        return make_response("OK", 200)
    
    if os.environ.get("ENV") == "development":
        # Cannot return response, just pass it so that the other handlers can deal with it
        # return make_response("Hello", 200)
        logger.info(request.endpoint)
        logger.debug(request.headers)
        pass
    else:
        func = app.view_functions[request.endpoint]
        if hasattr(func, '_exclude_from_check'):
            logger.info(f"{func} does not require auth check")
            pass
        else:
            path = "A"
            headers = {"Authorization": request.headers.get("Authorization")}
            res = requests.get(f"auth service URL here", params={"path":path}, headers=headers)
            # code cut for brevity

文件上传路径如下:

@bb.route("/add", methods=['POST'])
def create():

    # cut for brevity

    if 'file' not in request.files:
        status['status'] = "error"
        status['message'] = "Missing File upload"
        return make_response(status, 400)
    # cut for brevity


我遇到了一个奇怪的事件,即 request.files 是一个空的不可变字典,即使在向

/add
端点发出 POST 分段上传请求导致 400 响应之后也是如此。但是,如果我要在这里添加
logger.info(request.files)

    
   else:
       logger.info(request.files)
       func = app.view_functions[request.endpoint]

文件上传神奇。我试过放在其他位置,比如在这个修饰函数的末尾

check_if_auth()
,文件上传失败。有谁知道为什么会发生这种情况?

关于环境的更多信息:

  • 有点像在 docker 环境中。
  • 我正在经营服务员。
flask python-3.7
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.