在 GET 请求时,如果没有参数,可以使用 request.args.get() 方法来获取参数的值,如果没有参数,则返回 None。\n\n你可以尝试以下修改代码:\n\npython\ndef register_request_handlers(app: Flask):\n @app.before_request\n def log_request_body():\n app.logger.info('Record Request Information Start ...')\n if request.method == 'GET':\n param_value = request.args.get('param_name')\n if param_value is not None:\n app.logger.info(f'Request Args is {param_value}')\n elif request.method == 'POST':\n app.logger.info(f'Request Body is {request.json}')\n app.logger.info('End of recording request information ...')\n\n\n这样,如果 GET 请求中没有参数 param_name,则不会记录日志,也不会报错。

Flask 应用程序:处理 GET 请求中缺少参数的错误

原文地址: https://www.cveoy.top/t/topic/pVTo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录