What is Http 400 Bad Request and what causes it to happen?
What is method I can use to know which key
in request.form[key]
that cause bad request and how can I prevent it?
Updated
As Gerand mentioned in his comment:
This error happens when you are requesting a file through http which doesn't exist [....]
To make it clearer, here my sample code that cause Bad Request
:
hello.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)
hello.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>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" placeholder="username">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
From code above, the browser return Bad Request - The browser (or proxy) sent a request that this server could not understand.
without giving clue that which key that cause this error.
Therefore, which method I can use to know which key
that cause this error, and how can I prevent it?
Thanks.