REST resources पर क्रिया करने वाले verbs के रूप में standard HTTP methods इस्तेमाल करता है। CRUD (Create, Read, Update, Delete) से आम mapping यह है:
REST resources पर क्रिया करने वाले verbs के रूप में standard HTTP methods इस्तेमाल करता है। CRUD (Create, Read, Update, Delete) से आम mapping यह है:
| CRUD | Method | सामान्य उपयोग |
|---|
| Create | POST | एक collection में नया resource जोड़ना |
| Read | GET | एक resource या list लाना |
| Update (पूर्ण) | PUT | एक resource को पूरी तरह बदलना |
| Update (आंशिक) | PATCH | कुछ fields संशोधित करना |
| Delete | DELETE | एक resource हटाना |
POST /users HTTP/1.1
Content-Type: application/json
{ "name": "Ann", "email": "[email protected]" }
HTTP/1.1 201 Created
Location: /users/42
{ "id": 42, "name": "Ann", "email": "[email protected]" }
PUT /users/42 पूरी representation भेजता है और उसे बदल देता है; PATCH /users/42 केवल बदलने वाले fields भेजता है (उदा. { "email": "[email protected]" })।
ये दो अलग गुण हैं:
GET, HEAD, OPTIONS safe हैं।GET, PUT, DELETE, HEAD idempotent हैं।Method Safe? Idempotent?
GET yes yes
HEAD yes yes
PUT no yes (replacing with the same body twice = same result)
DELETE no yes (deleting twice = still deleted)
PATCH no no* (depends on the patch; not guaranteed)
POST no no (posting twice usually creates two resources)
Interviewers यह देखने के लिए पूछते हैं कि आप HTTP semantics को इतनी अच्छी तरह समझते हैं या नहीं कि विश्वसनीय APIs बना सकें। Safe/idempotent का भेद अकादमिक नहीं है: यह सीधे यह तय करता है कि clients, proxies, और CDNs को क्या करने की अनुमति है। Safe methods को स्वतंत्र रूप से cache और prefetch किया जा सकता है; idempotent methods को network timeout के बाद side effects को दोहराने के जोखिम के बिना स्वचालित रूप से retry किया जा सकता है — यही कारण है कि एक client PUT या DELETE को सुरक्षित रूप से retry कर सकता है पर POST को retry करते समय सावधान रहना चाहिए (यह दो orders बना सकता है)। Classic गलती है side effects trigger करने के लिए GET का उपयोग (उदा. GET /users/42/delete), जो caching को तोड़ता है और crawlers को गलती से data बदलने देता है। सही method चुनना — और उसके safety व idempotency contract का सम्मान करना — यही एक API को अनुमानयोग्य और उसके इर्द-गिर्द tooling बनाने के लिए सुरक्षित बनाता है।
विस्तृत उत्तरों के साथ IT इंटरव्यू प्रश्नों की एक लाइब्रेरी — जूनियर से सीनियर तक।
दान करें