Mapowanie metod HTTP na handlery klas
Zaimplementuj get, post, put i delete.
Mapowanie metod HTTP na handlery klas to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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! 🎉
Często zadawane pytania
Czy lekcja „Mapowanie metod HTTP na handlery klas” jest bezpłatna?
Tak — pełny tekst „Mapowanie metod HTTP na handlery klas” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Mapowanie metod HTTP na handlery klas”?
Zaimplementuj get, post, put i delete. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Flask Academy?
Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Mapowanie metod HTTP na handlery klas”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?
Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Klasy zasobów i obiekt Api
- Mapowanie metod HTTP na handlery klas
- Analizowanie i walidacja za pomocą reqparse
- Zasoby REST zamontowane w Blueprintach