Get and Post methods using Flask, Django, and FastAPI
Decoding the get and post methods in Python frameworks
HTTP provides different methods for communication between a client and a server. In the article, we're going to decode the basics of get and post methods using various Python frameworks.
Introduction
HTTP methods, also known as HTTP verbs, are an integral part of the Hypertext Transfer Protocol (HTTP). They define the purpose of a request made by a client to a server. The most common HTTP methods are GET, POST, PUT, PATCH, DELETE, and HEAD. Each HTTP method serves a specific purpose and is designed to perform different actions on resources.
Purpose of each method :
GET is used to retrieve data from a server
POST is used to send data to a server for processing
PUT is used to update or replace existing data
PATCH is used to partially modify existing data
DELETE is used to remove data from a server
Python frameworks like Flask, Django, and FastAPI are commonly used to set up an API for those backend servers.
Setting up the basic implementation of each framework
Flask
# ****************************Flask*********************************
from flask import Flask, request, jsonify
app = Flask(__name__) # creating an instance of Flask class
# ****************************FastAPI*********************************
from fastapi import FastAPI
app = FastAPI() # creating an instance of FastAPI class
# ****************************Django*********************************
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.urls import path
from django.core.wsgi import get_wsgi_application
from django.conf import settings
# In Django, no instance is created
Get Methods in each framework
# ****************************Flask*********************************
@app.route('/message', methods=['GET'])
def get_message():
message = 'This is the response for the GET request.'
return jsonify(message=message)
# **************************FastAPI*********************************
@app.get('/')
def get_message():
return "This is the response for GET request."
# ****************************Django*********************************
@csrf_exempt
def get_message(request):
return HttpResponse("This is the response for GET request.")
Post Methods in each framework
# ****************************Flask*********************************
@app.route('/', methods=['POST'])
def post_message():
message = request.form.get('message')
return f"The message you sent is: {message}"
# **************************FastAPI*********************************
@app.post('/')
def post_message(message: str):
return f"The message you sent is: {message}"
# ****************************Django*********************************
@csrf_exempt
def post_message(request):
if request.method == 'POST':
message = request.POST.get('message')
return HttpResponse(f"The message you sent is: {message}")
Running the server...
# ****************************Flask*********************************
app.run()
# **************************FastAPI*********************************
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)
# ****************************Django*********************************
from django.core.management import execute_from_command_line
execute_from_command_line(["manage.py", "runserver"])
For running the server, the above code and other details are written in this block if name == '__main__':
to ensure that the code inside it is executed only when the script is run directly as the main module, rather than being imported as a module into another script.
Generally, the servers are started from the command line
Conclusion
This was the basic application of a REST API server in these frameworks. Among these frameworks, Flask and FastAPI have simple and efficient source codes for implementation. The source code can easily be understood in these frameworks. For Django, the implementation can be a little complex for beginners to understand. It includes settings.configure
, urlpatterns
and other things.