Handling HTTP Methods (GET, POST, PUT, DELETE)0%
The Flask Request Object & Data Parsing

Handling HTTP Methods (GET, POST, PUT, DELETE)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Handling HTTP Methods (GET, POST, PUT, DELETE)

HTTP defines a set of request methods (also known as verbs) indicating the desired action to be performed on a given resource. By default, @app.route() only responds to GET requests. To build interactive web forms or REST APIs, routes must explicitly accept methods.


1. The methods Argument in @app.route

To allow multiple HTTP verbs on a route, supply a list to the methods parameter:

Python
from flask import Flask, request, jsonify
 
app = Flask(__name__)
 
@app.route("/api/v1/articles", methods=["GET", "POST"])
def manage_articles():
if request.method == "GET":
# Fetch and return list of articles
return jsonify([
{"id": 1, "title": "Flask Fundamentals"},
{"id": 2, "title": "REST APIs with Python"}
]), 200
 
elif request.method == "POST":
# Create a new article from request payload
data = request.get_json()
if not data or "title" not in data:
return jsonify({"error": "Title is required"}), 400
new_article = {"id": 3, "title": data["title"]}
return jsonify(new_article), 201

If a client sends an unsupported method (e.g., DELETE to /api/v1/articles), Flask automatically responds with an HTTP 405 Method Not Allowed header containing the allowed verbs.


2. Separate Route Handlers vs Consolidated Handlers

In RESTful design, you can either handle all methods in one view function using if request.method == '...', or separate them using modern route decorators:

Python
@app.route("/api/v1/articles/<int:article_id>", methods=["GET"])
def get_article(article_id):
return jsonify({"id": article_id, "title": "Sample Article"})
 
@app.route("/api/v1/articles/<int:article_id>", methods=["PUT"])
def update_article(article_id):
data = request.get_json()
return jsonify({"id": article_id, "updated_title": data.get("title")})
 
@app.route("/api/v1/articles/<int:article_id>", methods=["DELETE"])
def delete_article(article_id):
return "", 204

3. Method Semantics Overview

MethodIdempotentSafePrimary PurposeCommon Response Code
GETYesYesRetrieve representation of a resource without side effects200 OK
POSTNoNoSubmit data to create a new subordinate resource201 Created
PUTYesNoReplace the entire target resource with payload200 OK / 204 No Content
PATCHNoNoApply partial modifications to a resource200 OK
DELETEYesNoRemove the target resource permanently204 No Content
HEADYesYesSame as GET, but transfers status line and header section only200 OK
OPTIONSYesYesDescribe communication options for target resource (CORS preflight)204 No Content

4. Automatic HEAD and OPTIONS Handling

Flask handles HEAD and OPTIONS automatically:

  • Whenever you define a GET route, Flask automatically adds HEAD support. If a HEAD request arrives, Flask invokes the route handler, computes headers, but strips the response body before sending.
  • Flask automatically generates an OPTIONS response listing allowed verbs in the Allow HTTP header.

Interactive Quiz

Practice Quiz

Question 1
What HTTP methods are permitted by default when @app.route('/test') is defined without the methods argument?
Question 2
What status code does Flask return if a client sends an HTTP DELETE request to a route configured with methods=['GET', 'POST']?
Question 3
What is meant by an "idempotent" HTTP method?
Question 4
Which HTTP status code should be returned after successfully creating a new record via POST?
Question 5
How does Flask inspect the incoming request verb inside a view function?
Next Lesson

The Flask Request Object & Data Parsing

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext