← Back to HTML Course | Chapter 9: Reference & Advanced | Lesson 8 of 9

HTML HTTP मेथड्स

फ़र्ज़ करें आप किसी librarian से बात कर रहे हैं। अगर आपको कोई specific book पढ़नी है, तो आप उनसे shelf से लाने को कहते हैं (यह एक GET request है)। लेकिन अगर आपको library को कोई नई book donate करनी है, तो आप book को box में पैक करके उन्हें database में catalog करने के लिए दे देते हैं (यह एक POST request है)। HTML HTTP methods वही commands हैं। ये server को ठीक-ठीक बताते हैं कि browser किसी resource पर किस type का action करना चाहता है। सही method चुनना आपके user data को secure रखने, page loading speeds को optimize करने, और साफ-सुथरे web applications बनाने के लिए ज़रूरी है।

GET मेथड

GET method किसी server से data प्राप्त करने के लिए इस्तेमाल होता है। यह read-only और idempotent है, यानी एक जैसी request कई बार भेजने पर भी server पर कुछ भी बदले बिना वही result मिलता है।

Note: चूँकि GET data को URL में जोड़ता है, आप GET search result links को आसानी से bookmark या share कर सकते हैं।
Warning: passwords जैसा sensitive data भेजने के लिए GET method कभी इस्तेमाल न करें, क्योंकि वे browser address bar में खुले तौर पर दिख जाएंगे।

उदाहरण: The GET Method

markup
<form action="/search" method="get">
  <input type="text" name="q">
</form>

POST मेथड

POST method किसी resource को create या update करने के लिए server को data भेजने के लिए इस्तेमाल होता है। data URL में जोड़े जाने की बजाय HTTP request body के अंदर सुरक्षित तरीके से पैक किया जाता है।

Note: संवेदनशील जानकारी, file uploads, या database को बदलने वाले actions के लिए POST method का इस्तेमाल करें।
Warning: POST requests cache नहीं होतीं और users इन्हें bookmark नहीं कर सकते।

उदाहरण: The POST Method

markup
<form action="/upload" method="post">
  <input type="file" name="file">
</form>

PUT मेथड

PUT method किसी मौजूदा resource को update करने या न होने पर नया बनाने के लिए इस्तेमाल होता है। यह idempotent है, यानी एक ही update को कई बार भेजने पर भी वही final server state मिलता है।

Note: पूरे document या profile updates करते समय अपनी JavaScript fetch requests में PUT का इस्तेमाल करें।
Warning: Web browsers standard HTML forms के अंदर PUT को natively support नहीं करते; आपको इसे custom JavaScript से trigger करना होगा।

उदाहरण: The PUT Method

markup
<script>
  fetch('/api/profile/1', { method: 'PUT', body: JSON.stringify({ name: 'Alex' }) });
</script>

DELETE मेथड

DELETE method server से किसी specified resource को स्थायी रूप से हटाने के लिए इस्तेमाल होता है। PUT की तरह, इसे भी JavaScript fetch requests से ही trigger करना होता है क्योंकि यह HTML forms में natively supported नहीं है।

Note: files या user records को स्थायी रूप से हटाने के लिए अपनी scripts में DELETE requests का इस्तेमाल करें।
Warning: unauthorized data loss रोकने के लिए सुनिश्चित करें कि आपका server कोई भी DELETE request चलाने से पहले strict authentication ज़रूरी करे।

उदाहरण: The DELETE Method

markup
<script>
  fetch('/api/records/5', { method: 'DELETE' });
</script>

फॉर्म्स के लिए GET बनाम POST चुनना

GET और POST में चुनाव आपके form की प्रकृति पर निर्भर करता है। get non-sensitive search queries के लिए बेहतरीन है क्योंकि यह users को results bookmark और share करने देता है।

logins, registration, और file uploads के लिए POST ज़रूरी है, क्योंकि यह data को सुरक्षित और छिपा हुआ रखता है।

Note: एक अंगूठे के नियम के तौर पर, data प्राप्त करने के लिए GET और data submit या modify करने के लिए POST इस्तेमाल करें।
Warning: files या password keys भेजने के लिए GET इस्तेमाल करना URL length limits और security risks की वजह से fail हो जाएगा।

उदाहरण: Choosing GET vs. POST for Forms

markup
<form action="/search" method="get">
  <input type="text" name="q">
</form>
<form action="/register" method="post">
  <input type="password" name="password">
</form>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.