如何在 Python/Flask 中故意引起 400 Bad Request?
How to intentionally cause a 400 Bad Request in Python/Flask?
我的 REST API 的一位消费者说我有时会返回 400 Bad Request
- The request sent by the client was syntactically incorrect.
错误。
我的应用程序 (Python/Flask) 日志似乎没有捕捉到这一点,我的 webserver/Nginx 日志也没有。
编辑:出于调试目的,我想尝试在 Flask 中引发 400 错误请求。我该怎么做?
根据 James 的建议,我添加了类似于以下内容的内容:
@app.route('/badrequest400')
def bad_request():
return abort(400)
当我调用它时,flask returns 下面的 HTML,它不使用 "The request sent by the client was syntactically incorrect" 行:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
(我不确定为什么它不包括 <body>
标签。
在我看来,400 错误消息有不同的变体。例如,如果我将 cookie 的值设置为 50,000 的长度(使用带有 Postman 的拦截器),我将从 Flask 收到以下错误:
<html>
<head>
<title>Bad Request</title>
</head>
<body>
<h1>
<p>Bad Request</p>
</h1>
Error parsing headers: 'limit request headers fields size'
</body>
</html>
有没有办法让 Flask 解决 400 错误的不同变体?
您可以使用 abort
通过状态代码引发 HTTP 错误。
from flask import abort
@app.route('/badrequest400')
def bad_request():
abort(400)
您可以 return 将状态代码作为 return
的第二个参数,请参见下面的示例
@app.route('/my400')
def my400():
code = 400
msg = 'my message'
return msg, code
您还可以使用 abort
自定义消息错误:
from flask import abort
abort(400, 'My custom message')
见https://flask-restplus.readthedocs.io/en/stable/errors.html
此外,您可以使用 jsonify
from flask import jsonify
class SomeView(MethodView):
def post(self, *args, **kwargs):
if "csv_file" not in request.files:
return jsonify({'errors': 'No csv_file key in request.files.'}), 400
我的 REST API 的一位消费者说我有时会返回 400 Bad Request
- The request sent by the client was syntactically incorrect.
错误。
我的应用程序 (Python/Flask) 日志似乎没有捕捉到这一点,我的 webserver/Nginx 日志也没有。
编辑:出于调试目的,我想尝试在 Flask 中引发 400 错误请求。我该怎么做?
根据 James 的建议,我添加了类似于以下内容的内容:
@app.route('/badrequest400')
def bad_request():
return abort(400)
当我调用它时,flask returns 下面的 HTML,它不使用 "The request sent by the client was syntactically incorrect" 行:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
(我不确定为什么它不包括 <body>
标签。
在我看来,400 错误消息有不同的变体。例如,如果我将 cookie 的值设置为 50,000 的长度(使用带有 Postman 的拦截器),我将从 Flask 收到以下错误:
<html>
<head>
<title>Bad Request</title>
</head>
<body>
<h1>
<p>Bad Request</p>
</h1>
Error parsing headers: 'limit request headers fields size'
</body>
</html>
有没有办法让 Flask 解决 400 错误的不同变体?
您可以使用 abort
通过状态代码引发 HTTP 错误。
from flask import abort
@app.route('/badrequest400')
def bad_request():
abort(400)
您可以 return 将状态代码作为 return
的第二个参数,请参见下面的示例
@app.route('/my400')
def my400():
code = 400
msg = 'my message'
return msg, code
您还可以使用 abort
自定义消息错误:
from flask import abort
abort(400, 'My custom message')
见https://flask-restplus.readthedocs.io/en/stable/errors.html
此外,您可以使用 jsonify
from flask import jsonify
class SomeView(MethodView):
def post(self, *args, **kwargs):
if "csv_file" not in request.files:
return jsonify({'errors': 'No csv_file key in request.files.'}), 400