title: What is the cause of the Bad Request Error when submitting form in Flask application?
body: <p>After reading many similar sounding problems and the relevant Flask docs, I cannot seem to figure out what is generating the following error upon submitting a form:</p>
  <blockquote>
  <p>400 Bad Request</p>
  <p>The browser (or proxy) sent a request that this server could not understand.</p>
  </blockquote>
  <p>While the form always displays properly, the bad request happens when I submit an HTML form that ties to either of these functions:</p>
  <pre><code>@app.route('/app/business', methods=['GET', 'POST'])
  def apply_business():
      if request.method == 'POST':    
          new_account = Business(name=request.form['name_field'], email=request.form['email_field'], account_type=&quot;business&quot;, 
              q1=request.form['q1_field'], q2=request.form['q2_field'], q3=request.form['q3_field'], q4=request.form['q4_field'], 
              q5=request.form['q5_field'], q6=request.form['q6_field'], q7=request.form['q7_field'],
              account_status=&quot;pending&quot;, time=datetime.datetime.utcnow())
          db.session.add(new_account)
          db.session.commit()
          session['name'] = request.form['name_field']    
          return redirect(url_for('success'))
      return render_template('application.html', accounttype=&quot;business&quot;)          
  
  @app.route('/app/student', methods=['GET', 'POST'])
  def apply_student():    
      if request.method == 'POST':    
          new_account = Student(name=request.form['name_field'], email=request.form['email_field'], account_type=&quot;student&quot;, 
              q1=request.form['q1_field'], q2=request.form['q2_field'], q3=request.form['q3_field'], q4=request.form['q4_field'], 
              q5=request.form['q5_field'], q6=request.form['q6_field'], q7=request.form['q7_field'], q8=request.form['q8_field'], 
              q9=request.form['q9_field'], q10=request.form['q10_field'],
              account_status=&quot;pending&quot;, time=datetime.datetime.utcnow())
          db.session.add(new_account)
          db.session.commit()
          session['name'] = request.form['name_field']    
          return redirect(url_for('success')) 
      return render_template('application.html', accounttype=&quot;student&quot;)
  </code></pre>
  <p>The relevant part of HTML is</p>
  <pre class="lang-html prettyprint-override"><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;apply&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      {% if accounttype==&quot;business&quot; %}
      &lt;form action=&quot;{{ url_for('apply_business') }}&quot; method=post class=&quot;application_form&quot;&gt;
      {% elif accounttype==&quot;student&quot; %}
      &lt;form action=&quot;{{ url_for('apply_student') }}&quot; method=post class=&quot;application_form&quot;&gt;     
      {% endif %} 
      &lt;p&gt;Full Name:&lt;/p&gt;
      &lt;input name=&quot;name_field&quot; placeholder=&quot;First and Last&quot;&gt;
      &lt;p&gt;Email Address:&lt;/p&gt;
      &lt;input name=&quot;email_field&quot; placeholder=&quot;your@email.com&quot;&gt;
      ...
  </code></pre>
  <p>The problem for most people was not calling <code>GET</code> or <code>POST</code>, but I am doing just that in both functions, and I double checked to make sure I imported everything necessary, such as <code>from flask import request</code>. I also queried the database and confirmed that the additions from the form weren't added.</p>
  <p>In the Flask app, I was requesting form fields that were labeled slightly different in the HTML form. Keeping the names consistent is a must. More can be read at this question <a href="https://stackoverflow.com/questions/8552675/form-sending-error-flask">Form sending error, Flask</a></p>