【发布时间】:2015-11-27 08:01:41
【问题描述】:
我的 REST API 的使用者说我有时会返回 400 Bad Request - The request sent by the client was syntactically incorrect. 错误。
我的应用程序(Python/Flask)日志似乎没有捕捉到这一点,我的网络服务器/Nginx 日志也没有。
编辑:出于调试目的,我想尝试在 Flask 中引发 400 错误请求。我该怎么做?
按照 James 的建议,我添加了类似于以下内容的内容:
@app.route('/badrequest400')
def bad_request():
return abort(400)
当我调用它时,flask 返回以下 HTML,它没有使用“客户端发送的请求在语法上不正确”这一行:
<!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 的 Interceptor),我会从 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 错误的不同变体?
【问题讨论】:
标签: python rest nginx postman http-status-code-400