POST parameters with Flask

The usual way to send information to the different pages of our web application is by using HTML5 forms. It is advisable to use the POST method (the information is sent in the body of the request) for sending information using forms, although if necessary we can also use the GET method (the information is sent in the URL of the request).

Creating a form in a POST application will lead us to know how to control the data we upload. Here, we will look at an example of how we can handle the POST parameters with Flask.

The first way will be to create a route that accepts a GET request that returns a form that we will render using the render_template() method:

@app.route('/',methods=['GET'])
def index():
return render_template('index.html')

The template of the form will be very simple. The important thing is that the method is POST and the action field in the form object is pointing to the /validat. route.

You can find the following code in the index.html file inside the post_parameters folder:

<form action="/validate" method="post">
<label for="user">User</label>
<input type="text" id="user" name="user"><br/>

<label for="password">Password</label>
<input type="password" id="password" name="password"><br/>

<input type="submit" value="Submit" />
</form>

It is very important to put the name attributes in the form, since it will be that attribute that we use to recover the value. Now, we will create the route that accepts the POST requests. If a URL receives information through the POST method and we do not want it to be accessed with a GET method, it will be defined as follows:

@app.route('/validate',methods=['POST'])
def validate():

To access the information of the attributes of the form, we can use the request.form object. This object has attributes in a collection. So, we will retrieve the value of the user and password fields of the form, as you can see in the following script.

You can find the following code in the flaskapp_post.py file inside the post_parameters folder:

#!/usr/local/bin/python3
from flask import Flask, request, render_template
import json

app = Flask(__name__)
app.debug = True

@app.route('/',methods=['GET'])
def index():
return render_template('index.html')

@app.route('/validate',methods=['POST'])
def validate():
user = request.form['user']
password = request.form['password']
if user == 'admin' and password == 'password':
response = {'user_validate':True,'message':'User authenticated'}
else:
response = {'user_validate':False,'message':'User incorrect'}
return json.dumps(response)

if __name__ == '__main__':
app.run()

Once the method has been performed and the client has sent the values, we would have an answer like this. If the user and passwords match when defined in the code, it will return {"user_validate": true, "message": "User authenticated"}, otherwise it will return {'user_validate':False,'message':'User incorrect'}.

In this way, we have already seen how to manipulate and recover POST parameters with Flask.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset