Handling All Kinds of HTTP Methods in Flask (with List Storage)

Now that you’ve created your first Flask app, let’s learn how to store and manage data using a list — like a mini database!

We’ll also explore the 4 main HTTP methods:

  • GET – to read data
  • POST – to create new data
  • PUT – to update existing data
  • DELETE – to delete data

Let’s create a simple app to manage a list of tasks.


🧱 Step 1: Setup a Basic Flask App

Create a new file called app.py and copy this:

from flask import Flask, request, jsonify

app = Flask(__name__)

# Our in-memory data store
tasks = [
    {'id': 1, 'title': 'Buy groceries'},
    {'id': 2, 'title': 'Learn Flask'}
]

📥 GET Method – Read All Tasks

Add this route:

@app.route('/tasks', methods=['GET'])
def get_tasks():
    return jsonify(tasks)

Now visit http://127.0.0.1:5000/tasks in your browser — you’ll see the task list as JSON.


➕ POST Method – Add a New Task

Add this below the get_tasks() route:

@app.route('/tasks', methods=['POST'])
def add_task():
    data = request.get_json()
    new_id = tasks[-1]['id'] + 1 if tasks else 1
    new_task = {
        'id': new_id,
        'title': data['title']
    }
    tasks.append(new_task)
    return jsonify(new_task), 201

Try it with Postmancurl, or a browser extension like REST Client.

Sample POST request:

POST /tasks
Content-Type: application/json

{
  "title": "Practice Python"
}

✏️ PUT Method – Update an Existing Task

@app.route('/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
    data = request.get_json()
    for task in tasks:
        if task['id'] == task_id:
            task['title'] = data['title']
            return jsonify(task)
    return jsonify({'error': 'Task not found'}), 404

Sample PUT request:

PUT /tasks/1
Content-Type: application/json

{
  "title": "Buy groceries and veggies"
}

❌ DELETE Method – Remove a Task

@app.route('/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
    global tasks
    tasks = [task for task in tasks if task['id'] != task_id]
    return jsonify({'message': 'Task deleted'})

Sample DELETE request:

DELETE /tasks/2

🧪 Full Working Example (app.py)

from flask import Flask, request, jsonify

app = Flask(__name__)

tasks = [
    {'id': 1, 'title': 'Buy groceries'},
    {'id': 2, 'title': 'Learn Flask'}
]

@app.route('/tasks', methods=['GET'])
def get_tasks():
    return jsonify(tasks)

@app.route('/tasks', methods=['POST'])
def add_task():
    data = request.get_json()
    new_id = tasks[-1]['id'] + 1 if tasks else 1
    new_task = {
        'id': new_id,
        'title': data['title']
    }
    tasks.append(new_task)
    return jsonify(new_task), 201

@app.route('/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
    data = request.get_json()
    for task in tasks:
        if task['id'] == task_id:
            task['title'] = data['title']
            return jsonify(task)
    return jsonify({'error': 'Task not found'}), 404

@app.route('/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
    global tasks
    tasks = [task for task in tasks if task['id'] != task_id]
    return jsonify({'message': 'Task deleted'})

if __name__ == '__main__':
    app.run(debug=True)

🧠 Summary

  • GET: View the list of tasks
  • POST: Add a task to the list
  • PUT: Edit an existing task
  • DELETE: Remove a task

This is a simple, no-database way to learn how a REST API works using just Python lists!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top