How to intentionally cause a 400 Bad Request in Python/Flask?(如何在 Python/Flask 中故意导致 400 错误请求?)
问题描述
A consumer of my REST API says that on occasion I am returning a 400 Bad Request
- The request sent by the client was syntactically incorrect.
error.
My application (Python/Flask) logs don't seem to be capturing this, and neither do my webserver/Nginx logs.
Edit: I would like to try to cause a 400 bad request in Flask for debugging purposes. How can I do this?
Following James advice, I added something similar to the following:
@app.route('/badrequest400')
def bad_request():
return abort(400)
When I call this, flask returns the following HTML, which doesn't use the "The request sent by the client was syntactically incorrect" line:
<!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>
(I'm not sure why it isn't including the <body>
tags.
It appears to me that there are different variations of the 400 error message. For example, if I set a cookie to a value of length 50,000 (using Interceptor with Postman), I'll get the following error from Flask instead:
<html>
<head>
<title>Bad Request</title>
</head>
<body>
<h1>
<p>Bad Request</p>
</h1>
Error parsing headers: 'limit request headers fields size'
</body>
</html>
Is there a way to get Flask to through the different variations of 400 errors?
You can use abort
to raise an HTTP error by status code.
from flask import abort
@app.route('/badrequest400')
def bad_request():
abort(400)
这篇关于如何在 Python/Flask 中故意导致 400 错误请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python/Flask 中故意导致 400 错误请求?


- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01