Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is HTTP 400 Bad Request in Flask

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.

like image 521
rean computer Avatar asked Oct 29 '22 01:10

rean computer


1 Answers

Flask uses the werkzeug library's MultiDict data structure to hold POST data.

If you look at the implementation of MultiDict.__getitem__, you can see that if a key is not found, it will raise BadRequestKeyError with the name of the key as an argument. So you can inspect the exception's args attribute to get the name of the bad key:

from werkzeug.exceptions import BadRequestKeyError

@app.route('/hello', methods=['GET', 'POST'])
def hello():
    if request.method == 'POST':
        username = request.form['username']
        try:
            bad_key = request.form['bad_key']
        except BadRequestKeyError as ex: 
            return 'Unknown key: "{}"'.format(ex.args[0]), 500 

Note that while BadRequestKeyErrors string representation is

400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

the status of the response is actually

500 Internal Server Error

The keys of request.form correspond to the name attributes of the input tags in the form(s) in the HTML page being POSTed. Thus a BadRequestKeyError will often be caused by a mismatch between the name attributes in the HTML and the names expected by request.form['some_name'] in the route.

like image 166
snakecharmerb Avatar answered Nov 15 '22 05:11

snakecharmerb