Part 5 - Get and Post requests in Flask

October 12th, 2020 5 min read

Get Requests

Get requests are the requests that are passed in url, for example https://www.google.com/search?q=search_query is a get request to google.com’s search route. We are passing our search_query to the the google’s backend vai url. Get methods can also be in diffrent type such as https://newswebsite.com/this-is-a-latest-news/ .

When to use GET requests:

  1. When the request will not return diffrent data. ( Eg. Route of a website https://www.someblog.com/blog/blog-name/)
  2. When the request is safe , that is it will not change data. (Eg. Google Search https://www.google.com/search?q=search_query)

In this section we are going to see how to use get requests in flask. Now let’s first see a simple html form ,

<form action="/search" method="GET">
    <input name="q" value="search_term" placeholder="Search Here">
    <button type="submit"> Search </button>
</form>

As you may see it is a simple html form with search input box which is going to submit its query to the path in in its action attibute, which here is /search route of our application.

Note : In above form we have set method as GET so data will pass in url structure as a get request. The name of the input will be passed in the url.

For Eg. https://localhost:5000/search?q=search_term

Now the question lies how do we get this data in our flask application and use it, the answer is simple. See the example below,

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def Home():
	return render_template("home.html")


@app.route("/search")
def Search():
	if request.method == "GET":
		search_term = request.args.get("q")
		return search_term

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

In this example you may see that we have imported a new import that is request which we can use to access the request data by using this format request.args.get("name") where name should be replaced by the name of the input in form.

Here home.html renders the form that we saw above, and then the application get the value of the given input and passes the returns it.

Now let’s see POST requests in flask.

Post Requests

Get requests are the requests that are passed in not url, for example https://someblog.com/blog-name/delete/ can be an example of a post request.

When to use POST requests:

  1. When changes are to made to data. ( Eg. Addation or deletion of blogs on a website)
  2. When the data is confidential. ( Eg. Login Data )

In this section we are going to see how to use pot requests in flask. Now let’s first see a simple html form ,

login.html

<form action="/search" method="POST">
    <input name="username" value="search_term" placeholder="Search Here">
    <input name="password" value="search_term" placeholder="Search Here">
    <button type="submit"> Login </button>
</form>

As you may see it is a simple html login form with username and password fields. We don’t want this data to go in wrong hands so we will use POST method.

Note : In above form we have set method as POST so data will not pass in url structure.

Now let’s create a login route, and see how to manage POST data in flask, first we need to allow POST method on login route so will pass this argument methods = ['GET', 'POST'] so to allow both GET and POST methods in the route. Now let’s see this all in action,

app.py

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/login")
def Search():
	if request.method == "POST":
		username = request.form['username']
		password = request.form['password']

		# Handel Your Login Here

		return "Your Response"

	return render("login.html")

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

As you may see our route is simple, first it checks whether the request method is POST or GET and if it is POST it allows it to get get form data by request.form['field_name'] where field_name is name of the input field , the data recived is of the form of key value pair, then our route handels the login logic ( assume ) and returns the response. Else if the request.method is not POST so it return login.html .

Note : Use request.form['username'] to get data when you know it exists and use request.form.get('username') when you are not sure it exists , if data exists second method will return data else it will return an empty string. We use request.form.get('username') in get request because we didn’t know whether data exists or not.


Important

This is a list of functions to get data in flask app that were not so imortant ( except request.files as it deserves its own post ) to get part of there own in the blog but they get notable mentions they are as follows.

Method Name Method Use
request.data Handels data with the mime type that flask doesn’t handel’s on its own.
request.args Get value from get ( in argument form ) from url. Eg : /search?1=search_term
request.value Combines the values from args and form and serves is as one.
request.form Get data from form in form of key value pair.
request.json It gets sumimmited data and converts it in json format and serves it to us
request.form.getlist(‘name’) use it when data with same key is subbimited multiple times, it will get only first value and return it

Hope you like it. 😊 If you liked it please share it.


You may also like: