0Pricing
Flask Academy · 课时

将 HTTP 方法映射到类处理器

实现 get、post、put 和 delete

将 HTTP 方法映射到类处理器 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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! 🎉

常见问题解答

「将 HTTP 方法映射到类处理器」课时是免费的吗?

是的 — 「将 HTTP 方法映射到类处理器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「将 HTTP 方法映射到类处理器」这节课中我会学到什么?

实现 get、post、put 和 delete 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「将 HTTP 方法映射到类处理器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 资源类与 Api 对象
  2. 将 HTTP 方法映射到类处理器
  3. 使用 reqparse 解析并验证
  4. 挂载在蓝图上的 REST 资源
← 返回 Flask Academy