Flask HTTP Method (original) (raw)

Last Updated : 19 Mar, 2025

In this article, we will learn how to handle HTTP methods, such as GET and POST in Flask using Python. Before starting let’s understand the basic terminologies:

For a deeper understanding, read Flask HTTP methods, Handle GET & POST requests

Flask HTTP Methods

HTTP methods are rules that define how clients and servers communicate in a client-server architecture. They allow browsers or apps to send requests and receive responses. For example, when you search on Google, your browser sends a request, and Google’s server returns results.

Lets discuss the most common HTTP methods in detail.

GET Method

The GET method is used to request data from a server. It appends data to the URL in a name-value pair format. GET should not be used for sensitive data since URLs are visible in browser history.

Example of GET in Flask

To understand how the GET method works, we’ll build a simple Flask app that takes a number as input, calculates its square, and displays the result. Our app will have three files:

  1. **app.py – Contains the Flask app code.
  2. **squarenum.html – The homepage where users enter a number and send it to the Flask app.
  3. **answer.html – Displays the calculated square of the number.

**Code for flask app

Python `

from flask import Flask, request, render_template

app = Flask(name)

@app.route('/square', methods=['GET']) def squarenumber(): num = request.args.get('num')

if num is None:  # No number entered, show input form
    return render_template('squarenum.html')
elif num.strip() == '':  # Empty input
    return "<h1>Invalid number. Please enter a number.</h1>"
try:
    square = int(num) ** 2
    return render_template('answer.html', squareofnum=square, num=num)
except ValueError:
    return "<h1>Invalid input. Please enter a valid number.</h1>"

if name == 'main': app.run(debug=True)

`

**Explanation:

**Code for squarenum.html

HTML `

Square Of Number!

Welcome to the Maths page!

Logic shapes every choice of our daily lives.
Logical thinking enables someone to learn and make decisions that affect their way of life. !

Enter a number :

`

**Code for answer.html file

HTML `

Answer Page!

Keep Learning Maths!

Square of number {{num}} is :{{squareofnum}}

`

Let’s run our app and see if it’s working, below are the snippets of our app on development server

sq1

Square of Number!

On clicking the Submit button, one can notice, the value entered, appended, to the URL and, the output displayed.

Calculation

Calculation Result

POST Method in Flask

The POST method is used to send data to the server for processing. Unlike GET, it does not append data to the URL. Instead, it sends data in the request body, making it a better choice for sensitive or large data.

Example of POST in Flask

We will modify our previous Flask app to use POST instead of GET. The app will still take a number, calculate its square, and display the result.

Our app will have the same three files the only changes are made in the app.py and squarenum.html file.**

**Code for app.py

Python `

from flask import Flask, request, render_template

app = Flask(name)

@app.route('/square', methods=['GET', 'POST']) def squarenumber(): if request.method == 'POST': num = request.form.get('num') if num.strip() == '': # Empty input return "

Invalid number

" square = int(num) ** 2 return render_template('answer.html', squareofnum=square, num=num) return render_template('squarenum.html')

if name == 'main': app.run(debug=True)

`

**Explanation:

**Code for squarenum.html file

The only changes made in this code is that the form action has been changed from **GET to **POST.

HTML `

Square Of Number!

Welcome to the Maths Page!

Enter a number to find its square:

<form method="POST" action="/square">
    <label>Enter a number: </label>
    <input type="text" name="num">
    <input type="submit" value="Calculate">
</form>

`

Below are the snippets of the live demonstration

square

Square of Number

On clicking the Submit button, the data is posted back, to the server, and, the result page is rendered. Please note, the value entered, is not visible in the URL now, as the method used, is POST.

answer-page

Answer Page

Related Articles: Difference between GET and POST