Skip to content

HTTP Methods

Every request you send to a server carries a method. The method is a word that tells the server what kind of action you want to take - not just where you are going, but what you want to do when you get there.

Think of it like walking up to a counter. The address of the counter is the URL. The method is what you say when you get there - "I want to see the menu", "I want to place an order", "I want to change my order", "I want to cancel".


The four methods you will use

Method What it means When you use it
GET Fetch data Loading a list, viewing a record
POST Send new data to be saved Adding a new patient
PUT Update an existing record Changing a patient's name
DELETE Remove a record Deleting a patient

URL parameters

Before going further, there is one thing you need to understand: URL parameters.

A URL parameter is a value that is part of the URL path itself. When you want to work on a specific record, you include its id directly in the URL:

/patients/abc-123

Here, abc-123 is the URL parameter. It is baked into the path. Flask extracts it and passes it to your function.

On the Flask side, you capture it like this:

@app.route('/patients/<string:id>', methods=['GET'])
def get_patient(id):
    # id is now "abc-123"
    ...

<string:id> is the placeholder. Flask fills it in from whatever is in the URL.


Query parameters

Query parameters are different. They are attached to the end of a URL with ? and are used for filtering or searching - not for identifying a specific record:

/patients?name=Alice

?name=Alice is a query parameter. The URL path is still just /patients. The ?name=Alice part is extra information the server can use to filter results.

In Flask, you read them like this:

name = request.args.get("name")

How Flask declares which methods a route accepts

By default, a Flask route only accepts GET. If you want it to accept POST, PUT, or DELETE, you add methods:

@app.route('/patients', methods=['GET'])
def get_all_patients():
    ...

@app.route('/patients', methods=['POST'])
def add_patient():
    ...

@app.route('/patients/<string:id>', methods=['PUT'])
def update_patient(id):
    ...

@app.route('/patients/<string:id>', methods=['DELETE'])
def delete_patient(id):
    ...

The same URL path can have different routes for different methods. Flask checks the method on each incoming request and calls the matching function.


Challenge

For each of these actions, write down which HTTP method you would use and why:

  1. Show a list of all patients
  2. Add a new patient to the database
  3. Change a patient's age
  4. Remove a patient from the database
  5. Search for patients whose name starts with "A"

For number 5 - would you use a URL parameter or a query parameter? Why?


← Understanding Fetch Exercises    Next: PookieDB Refresher →