English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
HTTP protocol is the foundation of Web data communication. It defines methods for retrieving different data from specified URLs.
The following table summarizes different HTTP methods -
Number | Method | Description |
1 | GET | Send data to the server in an unencrypted form, which is the most commonly used method. |
2 | HEAD | Same as GET, but without a response body |
3 | POST | Used to send HTML form data to the server. Data received by the server via the POST method is not cached. |
4 | PUT | Replace all current representations of the target resource with the uploaded content. |
5 | DELETE | Delete all representations of the target resources specified by the URL |
By default, Flask routes respond to GET requests. However, this preference can be changed by providing method arguments to the route() decorator.
To demonstrate the use of the POST method in URL routing, first create an HTML form and send the form data to the URL using the POST method.
Save the following script to a file: login.html
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8-8"/> <title>Flask HTTP Request Methods</title>/title> </head> <body> <form action="http://localhost:5000/login" method="post"> <p>Enter name:/p> <p><input type="text" name="name" value=""/></p> <p><input type="submit" value="Submit" /></p> </form> </body> </html>
Now, input the following script in the Python shell.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 from flask import Flask, redirect, url_for, request app = Flask(__name__) @app.route('/success/<name>') def success(name): return 'welcome %s' % name @app.route('/login', methods = ['POST', 'GET']) def login(): if request.method == 'POST': user = request.form['name'] return redirect(url_for('success', name = user)) else: user = request.args.get('name') return redirect(url_for('success', name = user)) if __name__ == '__main__': app.run(debug = True)
After the development server starts running, open login.html in the browser, enter the name (such as: maxsu ) and click Submit.
Form data is submitted to the URL specified by the action attribute of the <form> tag.
http://localhost:5000/login is mapped to the login() function. Since the server has received data through the POST method, the value of the 'name' parameter obtained from the form data is passed in the following way:-
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 user = request.form['name']
It is passed as a variable part to the URL:/success. The browser displays a welcome message in the window.
Change the method parameter in login.html to GET and open it in the browser again. The data received on the server is through the GET method. The value of the 'name' parameter is now obtained in the following way: -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : www.oldtoolbag.com # Date : 2020-08-08 User = request.args.get('name')
Here, args is a dictionary object, which contains a series of form parameters and their corresponding values. As before, the value corresponding to the 'name' parameter will be passed to the URL:/success.