In this flask, we are going to learn all about flask HTTP methods which are mainly used in flask applications. In our previous Flask tutorial, we covered all about Flask URL building using various examples.
All web applications use different types of HTTP methods when accessing URLs. In this guide, you will be familiar with some HTTP methods which are mostly used in web applications. By default, HTTP method is the GET method.
Note:- To understand the flask coding you should have basic knowledge of Python programming tutorial.
Headings of Contents
Flask HTTP Methods:
Here we listed some HTTP methods but not all with their description. Here we listed some HTTP methods but not all with their description.
- GET:- The GET method is used to retrieve information from the server.
- POST:- POST is an HTTP request method that is used to post the data as a request body to the server.
- PUT:- PUT is an HTTP method that is used to update or modify the existing resource on the server.
- PATCH:- PATCH is an HTTP request method that is used to partially update the resource.
- DELETE:- The DELETE method is used to delete the existing resources from the server.
Let’s understand all the above Flask HTTP methods method with the help of examples.
GET Method:
To handle the GET request in the flask application. you should have to pass a GET in the method’s parameter of the route() method which is capable of retrieving information from the server.
Example:
from flask import Flask, url_for
#Create the object of the Flask class.
app= Flask(__name__)
@app.route("/users", methods=['GET'])
def Home():
#Assume below information coming from the server
return { "id": 1,"name": "John Doe", "email": "[email protected]"}
if __name__ == '__main__':
app.run(debug=True)
When you run the above flask script and visit http://127.0.0.1:5000/users address, Then you will get the following result.
{
"email": "[email protected]",
"id": 1,
"name": "John Doe"
}
POST Method:
The POST request method is used to post the data to the server. In this example, we send the JSON data to the server. You can use Postman for this request testing.
Example:
Suppose we have JSON data that contains information about one student.
{
"email": "[email protected]",
"id": 1,
"name": "John Doe"
}
Access all the JSON data.
from flask import Flask, url_for
#Create the object of the Flask class.
app= Flask(__name__)
@app.route("/users", methods=['POST'])
def Home():
#suppose json data save into the database.
data = request.json
return data
if __name__ == '__main__':
app.run(debug=True)
When you execute the above flask script, Then and visit with post request with request data you will get the following output.
{
"email": "[email protected]",
"id": 1,
"name": "John Doe"
}
Conclusion
Some Other Flask HTTP methods are described Flask database tutorial. I hope You don’t have any confusion regarding Python Flask HTTP methods.
HTTP methods are the major points for all web applications when URLs are accessed. If you want to learn flask from scratch to advanced, then follow our Python flask tutorial.
If you like this article, please share and keep visiting for further flask tutorials.
Python Flask Tutorials