-1

I have the following route that is getting values from a submitted form

@app.route('/authenticate', methods=["POST"])
def authenticate():
    username = request.form['username']
    print(username, file = sys.stderr)

    password = request.form['password']
    email = request.form['email']

    models.User.create_user(email, password, username)

    return render_template('signup.html')

The problem I'm running into is that I'm getting a Bad Request The browser (or proxy) sent a request that this server could not understand.I have checked that I am correctly getting the values from the form, and that all the forms have content in them, but It's not seeming to work.

Here is the template that renders the view

<form action ="/authenticate" method="POST" id="signup">
                <fieldset class="form-group">
                    <label for="InputUsername"> Username </label>
                    <input type="text" class="form-control" name="username" id="InputUsername" placeholder="Enter username">
                </fieldset>
                <fieldset class="form-group">
                    <label for="exampleInputEmail1">Email address</label>
                    <input type="email" class="form-control" name="email"id="exampleInputEmail1" placeholder="Enter email">
                    <small class="text-muted">We'll never share your email with anyone else.</small>
                  </fieldset>
                  <fieldset class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" name="password "id="exampleInputPassword1" placeholder="Password">
                </fieldset>
            </form>

and here is my view class that the forms are on

@app.route('/signup')
def login():
    return render_template("signup.html")
Rafa
  • 3,219
  • 4
  • 38
  • 70

1 Answers1

2

99% of the time, this error is a key error caused by your requesting a key in the request.form dictionary that does not exist. To debug it, run

print(request.form)

and make sure that every key you request is present in the dictionary. This error is particularly frustrating (and seemingly mysterious) because it doesn't trigger the usual traceback you normally get in Flask.

Check out these questions:

What is the cause of the Bad Request Error .. ?
Form sending error, Flask

Community
  • 1
  • 1
T. Arboreus
  • 1,067
  • 1
  • 9
  • 17
  • This is what I'm getting printed out, but I'm calling only these values. ImmutableMultiDict([('username', u'ralphie9224'), ('password ', u'MAVS.man1'), ('email', u'rmoreno.cesar@gmail.com')]) – Rafa May 04 '16 at 01:45
  • Ah. Use the same name for all the inputs but add different values. Something like this: If it works I'll edit my answer. – T. Arboreus May 04 '16 at 01:52