🏫 The School›🏢 APIs›13 · 🔧 प्रत्येक REST method — सात शिक्के
🖼️ See the drawing + lab 🏠 Course home 🌿 Branch on GitHub ✏️ View source
🖼️ आकृती आणि labThe drawing + lab पूर्ण पानावर उघडा ↗Open full page ↗

13 · 🔧 प्रत्येक REST method — सात शिक्के

भाग 3 — संपूर्ण नकाशे. धडे 13–16 हे संदर्भ धडे आहेत: या कोर्सने धडे 01–12 मध्ये बांधलेल्या एका काउंटरपलीकडे कामातल्या अभियंत्याला भेटणारे सगळे. प्रत्येकाचा site वर पूर्ण काढलेला नकाशा आणि या repo मध्ये चालवता येणारा code आहे.

📦 या ब्रँचमध्ये काय आहे

धडे 01–13. काउंटर आता सातही REST methods ना उत्तर देतो:

🧒 5 वर्षांच्या मुलाला समजावल्यासारखे

काउंटरकडे सात शिक्के आहेत. त्यातले तीन फक्त कपाटाकडे पाहतात — 📖 GET file वाचतो, 👀 HEAD file न उघडता ती आहे का ते पाहतो, ❓ OPTIONS विचारतो "मी इथे काय करू शकतो?". चार शिक्के कपाट बदलतात — ➕ POST नवीन form file करतो (आणि त्याला नंबर देतो), 🔁 PUT एक file पूर्ण नवीन file ने बदलतो, ✏️ PATCH file मधली एक ओळ बदलतो, 🗑️ DELETE file बाहेर काढतो.

तीन शब्द शिक्क्यांना वेगळे करतात. Safe: ते काही बदलते का? Idempotent: मी ते दोनदा केले तर कपाट एकदा केल्यासारखेच राहते का? Cacheable: उत्तर जपून पुन्हा वापरता येते का? तुमच्या आणि काउंटरमधले संपूर्ण internet — browsers, CDNs, proxies, retry libraries — या तीन वचनांवर विश्वास ठेवते. एक मोडले की तुम्ही सगळे एकदम मोडता.

🗺️ आकृती

flowchart LR
  subgraph safe["SAFE · changes nothing · cacheable"]
    G[GET — read it] ; H[HEAD — headers only] ; O[OPTIONS — what may I do?]
  end
  subgraph unsafe["UNSAFE · needs the hall pass"]
    P[POST — file a NEW one<br/>not idempotent] ; U[PUT — replace the whole thing<br/>idempotent] ; PA[PATCH — change part of it<br/>idempotent for merge-patch] ; D[DELETE — remove it<br/>idempotent by state]
  end
  C((the counter)) --> safe --> C
  C --> unsafe --> C

❓ काय

Method काय करते Safe Idempotent Body आत Cacheable यशस्वी उत्तर
GET एक resource किंवा यादी वाचते ✅ ✅ ❌ (query string) ✅ 200 · 304
HEAD GET चे फक्त headers ✅ ✅ ❌ ✅ 200 · 304
OPTIONS परवानगी असलेले methods · CORS preflight ✅ ✅ ❌ ✅ (Max-Age) 204 · 200
POST तयार करते · किंवा एखादी क्रिया करते ❌ ❌ Idempotency-Key नसेल तर ✅ ❌ 201 + Location · 200 · 202
PUT पूर्ण resource बदलते ❌ ✅ ✅ सगळे fields ❌ 200 · 204
PATCH resource चा एक भाग बदलते ❌ ✅ merge-patch साठी ✅ फक्त बदल ❌ 200 · 204
DELETE resource काढून टाकते ❌ ✅ स्थितीनुसार ❌ ❌ 204 · 202

TRACE आणि CONNECT RFC मध्ये आहेत; काउंटर ते जवळजवळ कधीच लागू करत नाही.

🤔 का

प्रत्येक मध्यस्थ या तीन शब्दांवर विश्वास ठेवतो. Delete करणारा GET एखादा crawler चालवून टाकतो. वाचण्यासाठी वापरलेला POST cache होऊ शकत नाही. Idempotent नसलेला PUT निरुपद्रवी retry ला बिघडलेल्या data मध्ये बदलतो. Method निवडणे म्हणजेच तुमच्या आणि तुमच्या client मधल्या प्रत्येक गोष्टीला कोणती वचने द्यायची ते निवडणे.

🔧 कसे (या repo मध्ये)

🧪 करून पाहा

python3 api/school_api.py &
bash api/methods_demo.sh
# then by hand:
curl -I http://127.0.0.1:8080/v1/students/2                                  # HEAD: headers, no body
curl -i -X OPTIONS http://127.0.0.1:8080/v1/students/2 | grep -i allow        # what may I do here?
curl -X PATCH http://127.0.0.1:8080/v1/students/2 -H 'X-API-Key: hall-pass-123' \
     -H 'Content-Type: application/json' -d '{"grade":"A"}'                  # one field only

✅ तपासा — तुम्हाला काय दिसायला हवे

methods_demo.sh प्रत्येक method साठी एक block छापतो: GET आणि HEAD वर तोच ETag, OPTIONS वर Allow यादी, दोन POSTs मधून दोन Location headers, दोन PUTs मधून एक student, एकाच field च्या PUT साठी problems सह 400, PATCH मधून merge झालेला student, अनोळखी PATCH field साठी 400, आणि DELETE, DELETE, GET साठी 204 204 404. पूर्ण मजकूर api/methods-output.txt मध्ये आहे.

🏁 तुम्ही आत्ताच काय सिद्ध केले

तुम्ही तीन शब्द तारेवर पाहिले: safe methods नी हॉल पासशिवाय उत्तर दिले, idempotent methods नी दोनदा तीच स्थिती दिली, आणि एकमेव non-idempotent method (POST) ने जुळे तयार केले — म्हणूनच धडा 07 ने Idempotency-Key जोडली.

⚠️ नेहमीच्या चुका

🏭 प्रत्यक्ष वापरात हे का महत्त्वाचे: retry libraries idempotent methods आपोआप retry करतात आणि POST कधीच retry करत नाहीत; CDNs फक्त GET आणि HEAD cache करतात, दुसरे काहीच नाही; browsers OPTIONS ने preflight करतात. Method बरोबर निवडा आणि तिघेही आपोआप योग्य वागतात. चुकीची निवडा आणि रात्री 2 वाजता deletion serve करणाऱ्या cache चा debug करत बसाल.

⏭️ पुढे

धडा 14 — प्रत्येक authentication पद्धत: काउंटरकडे एकाच प्रकारचा हॉल पास आहे; जगात नऊ आहेत.

13 · 🔧 Every REST method — the seven stamps

Part 3 — the full maps. Lessons 13–16 are the reference lessons: everything a working engineer meets beyond the one counter this course built in lessons 01–12. Each one has a full drawn map on the site and runnable code in this repo.

📦 What's in this branch

Lessons 01–13. The counter now answers all seven REST methods:

🧒 Explain like I'm 5

The counter has seven stamps. Three of them only look at the cabinet — 📖 GET reads a file, 👀 HEAD checks the file is there without opening it, ❓ OPTIONS asks "what may I do here?". Four of them change the cabinet — ➕ POST files a new form (and gives it a number), 🔁 PUT swaps a file for a whole new one, ✏️ PATCH changes one line on a file, 🗑️ DELETE takes a file out.

Three words tell the stamps apart. Safe: does it change anything? Idempotent: if I do it twice, is the cabinet the same as doing it once? Cacheable: may the answer be kept and reused? The whole internet between you and the counter — browsers, CDNs, proxies, retry libraries — believes those three promises. Break one and you break them all at once.

🗺️ Diagram

flowchart LR
  subgraph safe["SAFE · changes nothing · cacheable"]
    G[GET — read it] ; H[HEAD — headers only] ; O[OPTIONS — what may I do?]
  end
  subgraph unsafe["UNSAFE · needs the hall pass"]
    P[POST — file a NEW one<br/>not idempotent] ; U[PUT — replace the whole thing<br/>idempotent] ; PA[PATCH — change part of it<br/>idempotent for merge-patch] ; D[DELETE — remove it<br/>idempotent by state]
  end
  C((the counter)) --> safe --> C
  C --> unsafe --> C

❓ What

Method Does Safe Idempotent Body in Cacheable Happy answer
GET read a resource or a list ✅ ✅ ❌ (query string) ✅ 200 · 304
HEAD GET's headers only ✅ ✅ ❌ ✅ 200 · 304
OPTIONS allowed methods · CORS preflight ✅ ✅ ❌ ✅ (Max-Age) 204 · 200
POST create · or perform an action ❌ ❌ unless Idempotency-Key ✅ ❌ 201 + Location · 200 · 202
PUT replace the whole resource ❌ ✅ ✅ all fields ❌ 200 · 204
PATCH change part of the resource ❌ ✅ for merge-patch ✅ the changes ❌ 200 · 204
DELETE remove the resource ❌ ✅ by state ❌ ❌ 204 · 202

TRACE and CONNECT exist in the RFC; a counter almost never implements them.

🤔 Why

Every intermediary trusts the three words. A GET that deletes is deleted by a crawler. A POST used for reads cannot be cached. A PUT that is not idempotent turns a harmless retry into corrupted data. Choosing the method is choosing which promises you make to everything between you and your client.

🔧 How (in this repo)

🧪 Try it

python3 api/school_api.py &
bash api/methods_demo.sh
# then by hand:
curl -I http://127.0.0.1:8080/v1/students/2                                  # HEAD: headers, no body
curl -i -X OPTIONS http://127.0.0.1:8080/v1/students/2 | grep -i allow        # what may I do here?
curl -X PATCH http://127.0.0.1:8080/v1/students/2 -H 'X-API-Key: hall-pass-123' \
     -H 'Content-Type: application/json' -d '{"grade":"A"}'                  # one field only

✅ Verify — what you should see

methods_demo.sh prints one block per method: the same ETag on GET and HEAD, an Allow list on OPTIONS, two Location headers from two POSTs, one student from two PUTs, a 400 with problems for a one-field PUT, a merged student from PATCH, a 400 for an unknown PATCH field, and 204 204 404 for DELETE, DELETE, GET. The full text is in api/methods-output.txt.

🏁 What you just proved

You watched the three words on the wire: safe methods answered without a hall pass, idempotent methods gave the same state twice, and the one non-idempotent method (POST) made twins — which is exactly why lesson 07 added the Idempotency-Key.

⚠️ Common mistakes

🏭 Why this matters in production: retry libraries retry idempotent methods automatically and never retry POST; CDNs cache GET and HEAD and nothing else; browsers preflight with OPTIONS. Get the method right and all three do the right thing for free. Get it wrong and you debug a cache serving a deletion at 2 a.m.

⏭️ Next

Lesson 14 — every authentication method: the counter has one kind of hall pass; the world has nine.

← Previoustesting docs gatewayNext →auth methods

This page is the lesson's README from the lesson-13-rest-methods branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.