HTTPメソッドをクラスハンドラーに対応付ける
get、post、put、deleteを実装します
「HTTPメソッドをクラスハンドラーに対応付ける」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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}, 201Return a Status Code
Return a tuple of dict and number to set the status code. A fresh create should answer 201.
return {"id": 5}, 201Handle 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 "", 204URL 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メソッドをクラスハンドラーに対応付ける」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「HTTPメソッドをクラスハンドラーに対応付ける」で何を学びますか?
get、post、put、deleteを実装します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「HTTPメソッドをクラスハンドラーに対応付ける」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リソースクラスとApiオブジェクト
- HTTPメソッドをクラスハンドラーに対応付ける
- reqparseで解析と検証を行う
- BlueprintにマウントするRESTリソース