Forums

400 Bad Request Error Flask

Hi! I am getting a 400 bad request error when I go to a certain page in my web app. The code is copied below. Any help would be greatly appreciated! HTML Code:

      <form method="POST" action="/sendorder/ordertype">
        <div class="form-group">
        <label for="FormControlSelect1">Select Size</label>
        <select class="form-control" id="FormControlSelect1" name="SIZE">
          <option>youth small</option>
          <option>youth medium</option>
          <option>youth large</option>
        </select>
      </div>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="EMAIL">
    </div>
    <div class="form-group">
      <label for="add">Address:</label>
      <input type="text" class="form-control" id="add" placeholder="Enter address" name="ADDRESS">
    </div>
    <div class="form-group">
      <label for="note">Note:</label>
      <input type="text" class="form-control" id="note" placeholder="Enter note" name="NOTE">
    </div>
    <button type="submit" class="btn btn-info">Submit</button>
  </form>

Python/Flask Code:

@app.route('/sendorder/<order>', methods=['GET', 'POST'])
def sendOrder(order=None):

    if request.method == "POST":
        name = request.form['NAME']
        email = request.form['EMAIL']
        size = request.form.get('SIZE')
        note = request.form['NOTE']
        address = request.form['ADDRESS']

That looks like it should be working. Do you have some sort of csrf library installed? Have you reloaded your web app recently?

I'm not sure what a csrf library is, so probably not. And yes, I reloaded my web app just now and it still gives a 400: Bad Request Error. Sorry for the late response.

Ok. Then the only other thing I can think of is that perhaps that's not the view that is being run. Perhaps you have a different view that is the one that is actually getting the request. You can see which paths are being accessed and which ones are returning 400 errors. Then maybe you can use the to identify the one that is the issue.

I figured it out. I was trying to get data from a name text box, but I had forgotten to create the text box. Thank you!

Great! Glad you found the problem.

Hello, I have a same problem. I have android app, the app make post consults to server, and all works, but I recent add csrf = CSRFProtect(app), csrf.init_app(app), and them the post request not work aymore.

Then your app is not providing the appropriate CSRF headers to the web app. You will need to work out from the library that provides CSRFProtect how they handle CSRF so that you can send the appropriate CSRF information from your app.