Skip to content

HTTP Methods (GET vs POST)

HTTP methods tell the server what the client intends to do.

diagram one view, two methods, two jobs mermaid
A route that accepts both is the ordinary shape for a form. The GET renders it and the POST processes it, and the branch on request.method is what keeps them apart. A method the rule does not list gets 405 from the router before your code runs.

Use GET for reading:

  • fetching a page
  • querying data

Characteristics:

  • should be safe (no state change)
  • parameters usually appear in the URL (?q=...)
  • can be cached

Use POST for writing/changing state:

  • submitting forms
  • creating a new resource
  • login/register actions

Characteristics:

  • carries data in request body
  • not cached by default
  • can trigger CSRF protections (later)

By default, @app.route allows GET.

To accept POST:

python
from flask import Flask, request
 
app = Flask(__name__)
 
 
@app.route("/submit", methods=["GET", "POST"])
def submit():
    if request.method == "POST":
        return "Received POST", 201
 
    return "Submit page"
  • GET: “Give me the current representation of this resource.”
  • POST: “Process this data.”
  • Using GET for actions like /delete?id=5 (dangerous)
  • Forgetting to restrict methods → 405 Method Not Allowed in production
  • Not validating/escaping user input

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading