how to access form data using flask?
You are not using curl
correctly. Try like this:
curl -d 'uuid=admin&password=admin'
The 400 Bad Request
error is the usual behavior when you try to get nonexistent keys from request.form
.
Alternatively, use request.json
instead of request.form
and call curl
like this:
curl -d '{"uuid":"admin","password":"admin"}' -H "Content-Type: application/json"
You still need to return a response:
from flask import abort@app.route('/login/', methods=['GET', 'POST'])def login(): if request.method == 'POST': user = User.get(request.form['uuid']) if user and hash_password(request.form['password']) == user._password: login_user(user, remember=True) return redirect('/home/') else: return abort(401) # 401 Unauthorized else: return abort(405) # 405 Method Not Allowed
Here's the documentation for custom Flask error pages.
Also, take a look at Flask-Bcrypt for password hashing.
Your CURL command line isn't valid. JSON objects need to have double quotes around the keys and values:
$ curl -d '{"uuid": "admin", "password": "admin"}' http://127.0.0.1:5000/login/
Now, you can access the keys with request.json
.