什么是 Http 400 错误请求以及导致它发生的原因?
是什么方法,我可以用它来知道key在request.form[key]那造成不好的请求,我该如何预防呢?
更新
正如 Gerand 在他的评论中提到的:
当您通过不存在的 http 请求文件时会发生此错误 [....]
为了更清楚,这里是我的示例代码,导致Bad Request:
你好.py
# -*- coding: utf-8 -*-
from flask import *
import re
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def checkName():
return render_template('hello.html')
@app.route('/hello',methods=['GET','POST'])
def printName():
if request.method=='POST':
username = request.form['username']
bad_key = request.form['bad_key'] # this key is not exist
return "Hello, ",username
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
你好.html
<form class="form-horizontal" action='/hello' method='POST' name="frm_submit">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">User Name:</label> …Run Code Online (Sandbox Code Playgroud)