title: What is HTTP 400 Bad Request in Flask
body: <p>What is Http 400 Bad Request and what causes it to happen?</p>
  <p>What is method I can use to know which <code>key</code> in <code>request.form[key]</code> that cause bad request and how can I prevent it?</p>
  <p><strong>Updated</strong></p>
  <p>As Gerand mentioned in his comment:</p>
  <blockquote>
  <p>This error happens when you are requesting a file through http which
  doesn't exist [....]</p>
  </blockquote>
  <p>To make it clearer, here my sample code that cause <code>Bad Request</code>:</p>
  <p><strong>hello.py</strong></p>
  <pre><code># -*- 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 &quot;Hello, &quot;,username
  
  if __name__ == '__main__':
  
      app.run(debug=True)
  </code></pre>
  <p><strong>hello.html</strong></p>
  <pre><code>&lt;form class=&quot;form-horizontal&quot; action='/hello' method='POST' name=&quot;frm_submit&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
      &lt;label for=&quot;username&quot; class=&quot;col-sm-2 control-label&quot;&gt;User Name:&lt;/label&gt;
      &lt;div class=&quot;col-sm-10&quot;&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;username&quot; name=&quot;username&quot; placeholder=&quot;username&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;form-group&quot;&gt;
      &lt;div class=&quot;col-sm-offset-2 col-sm-10&quot;&gt;
        &lt;button type=&quot;submit&quot; class=&quot;btn btn-default&quot;&gt;Submit&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>From code above, the browser return <code>Bad Request - The browser (or proxy) sent a request that this server could not understand.</code> without giving clue that which key that cause this error.</p>
  <p>Therefore, which method I can use to know which <code>key</code> that cause this error, and how can I prevent it?</p>
  <p>Thanks.</p>