0Pricing
Flask Academy · Lesson

Map HTTP Methods to Class Handlers

Implement get, post, put, and delete.

Map HTTP Methods to Class Handlers is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Verbs Become Methods

In Flask-RESTful each HTTP verb maps to a same-named method on your Resource. GET calls get, POST calls post.

Handle a GET

Define a get method to read data. Returning a dict sends it back as JSON automatically.

class Task(Resource):
    def get(self, task_id):
        return {"id": task_id}

Handle a POST

Add a post method to create something. It runs only when a client sends a POST to that URL.

def post(self):
    return {"created": True}, 201

Return a Status Code

Return a tuple of dict and number to set the status code. A fresh create should answer 201.

return {"id": 5}, 201

Handle a PUT

A put method replaces or updates a resource. Clients send the full new state in the body.

def put(self, task_id):
    return {"updated": task_id}

Handle a DELETE

A delete method removes a resource. A common reply is an empty body with status 204.

def delete(self, task_id):
    return "", 204

URL Args Flow In

Path variables from add_resource arrive as method arguments, just like in normal Flask view functions.

api.add_resource(Task, "/tasks/<int:task_id>")

One Class, Many Verbs

Group get, put, and delete for a single item in one class. Each verb has its own focused method.

class Task(Resource):
    def get(self, id): ...
    def delete(self, id): ...

Two Resources for REST

A typical API uses a collection Resource at /tasks and an item resource at /tasks/<id>.

api.add_resource(TaskList, "/tasks")
api.add_resource(Task, "/tasks/<int:id>")

Unsupported Verbs Are Handled

If a client uses a verb you did not define, Flask-RESTful answers 405 for you. No extra code needed. 🙌

Add Headers if Needed

Return a third tuple item to set headers, like pointing to a newly created resource location.

return data, 201, {"Location": "/tasks/5"}

Quick Check

A client sends DELETE to your resource. Which method runs?

Recap: Verbs to Methods

You mapped HTTP verbs to methods, set status codes with tuples, and split collection and item resources. Full CRUD! 🎉

Frequently asked questions

Is the “Map HTTP Methods to Class Handlers” lesson free?

Yes — the full text of “Map HTTP Methods to Class Handlers” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Map HTTP Methods to Class Handlers”?

Implement get, post, put, and delete. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Map HTTP Methods to Class Handlers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Resource Classes and the Api Object
  2. Map HTTP Methods to Class Handlers
  3. Parse and Validate with reqparse
  4. Blueprint-Mounted REST Resources
← Back to Flask Academy