0
0
Flaskframework~10 mins

HTTP methods (GET, POST, PUT, DELETE) in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTTP methods (GET, POST, PUT, DELETE)
Client sends HTTP request
Server receives request
Check HTTP method
Read
Send response back to client
The server receives a request, checks the HTTP method (GET, POST, PUT, DELETE), performs the related action, then sends a response.
Execution Sample
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/item', methods=['GET', 'POST', 'PUT', 'DELETE'])
def item():
    if request.method == 'GET':
        return 'Read item'
    elif request.method == 'POST':
        return 'Create item'
    elif request.method == 'PUT':
        return 'Update item'
    elif request.method == 'DELETE':
        return 'Delete item'

if __name__ == '__main__':
    app.run(debug=True)
A Flask route that handles GET, POST, PUT, DELETE methods and returns a message for each. Note: Flask automatically returns 405 for unsupported methods like PATCH.
Execution Table
StepHTTP MethodCondition CheckedAction TakenResponse Sent
1GETrequest.method == 'GET'Return 'Read item''Read item'
2POSTrequest.method == 'POST'Return 'Create item''Create item'
3PUTrequest.method == 'PUT'Return 'Update item''Update item'
4DELETErequest.method == 'DELETE'Return 'Delete item''Delete item'
5PATCHRoute method check fails (before function)405 Method Not Allowed (Flask default)405 Method Not Allowed
💡 Execution stops after matching HTTP method condition. Unsupported methods like PATCH trigger Flask's 405 before entering the function.
Variable Tracker
VariableStartAfter GETAfter POSTAfter PUTAfter DELETEPATCH (not entered)
request.methodNone'GET''POST''PUT''DELETE''PATCH' (function not called)
Key Moments - 3 Insights
Why does the server return 'Read item' only when the method is GET?
Because in the execution_table row 1, the condition 'request.method == 'GET'' is true only for GET requests, triggering the 'Read item' response.
What happens if the client sends a PATCH request?
As shown in execution_table row 5, PATCH is not in the route's methods list, so Flask returns 405 Method Not Allowed before entering the function.
Why do we check the HTTP method inside the route function?
Because the same URL can handle different actions depending on the method (RESTful design), as shown in the execution_table where each method triggers a different response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response is sent when the HTTP method is PUT?
A'Create item'
B'Update item'
C'Delete item'
D'Read item'
💡 Hint
Check the row where HTTP Method is PUT in the execution_table.
At which step does the server return 'Delete item'?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look for the row with HTTP Method DELETE in the execution_table.
If the client sends a PATCH request, what will the server respond with?
A'Update item'
B'Delete item'
C405 Method Not Allowed
D'Create item'
💡 Hint
See the last row in the execution_table for unsupported methods.
Concept Snapshot
HTTP methods define RESTful actions on a resource:
GET = read/retrieve data
POST = create new data
PUT = update/replace existing data
DELETE = remove/delete data
In Flask, specify methods in @app.route() and check request.method inside for logic. Unsupported methods auto-return 405.
Full Transcript
This example shows how a Flask server handles different HTTP methods (GET, POST, PUT, DELETE) on the same '/item' URL following REST principles. The route specifies allowed methods. Inside the function, if-elif checks request.method to return appropriate messages: 'Read item' for GET, etc. For unsupported methods like PATCH, Flask returns 405 Method Not Allowed without calling the function.