Requests Module in hindi
Requests Module in Python in Hindi – Complete Table of Contents
Table of Contents
- Introduction to Requests Module in hindi
- Installation of Requests Module in hindi
- GET Request using Requests Module in hindi
- POST Request using Requests Module in hindi
- Response Object in Requests Module in hindi
- HTTP Headers in Requests Module in hindi
- Status Codes in Requests Module in hindi
- JSON Response Handling in Requests Module in hindi
- Error Handling in Requests Module in hindi
- Timeout in Requests Module in hindi
- Session Object in Requests Module in hindi
Requests Module in Python in Hindi – Complete Notes
आज के समय में Python का use सिर्फ programming तक सीमित नहीं है, बल्कि Web Development, Data Science और API Integration में भी बहुत ज्यादा होता है। जब हमें किसी website या server से data लेना या भेजना होता है, तब Python का Requests Module सबसे ज्यादा use किया जाता है। इस article में हम Requests Module in hindi को बिल्कुल basic level से exam-oriented तरीके में समझेंगे।
Introduction to Requests Module
Requests Module एक popular Python library है, जिसका use HTTP requests भेजने के लिए किया जाता है। इस module की help से हम आसानी से GET, POST, PUT, DELETE जैसे requests send कर सकते हैं।
College exams में अक्सर पूछा जाता है कि Requests Module क्या है और इसका use क्यों किया जाता है। Simple शब्दों में कहें तो यह module Python और Web Server के बीच communication को आसान बनाता है।
Requests Module developer-friendly है, क्योंकि इसमें complex code लिखने की जरूरत नहीं पड़ती। कुछ ही lines के code में हम किसी API या website से data fetch कर सकते हैं।
Why Requests Module is Important
- Web APIs से data fetch करने के लिए
- Server को data send करने के लिए
- REST API testing के लिए
- Automation scripts बनाने के लिए
Installation of Requests Module
Python में Requests Module default रूप से install नहीं होता। इसे use करने से पहले हमें इसे install करना पड़ता है।
Requests Module install करने के लिए pip command का use किया जाता है। यह तरीका exam और practical दोनों के लिए important है।
pip install requests
अगर installation successful है, तो Python program में इसे import करके use किया जा सकता है। Exam में कई बार पूछा जाता है कि Requests Module को import कैसे करते हैं।
import requests
अगर import के समय कोई error नहीं आता, तो इसका मतलब module सही तरीके से install हो चुका है। यह step practical exams में बहुत common है।
GET Request using Requests Module
GET Request का use server से data retrieve करने के लिए किया जाता है। जब हम किसी website का URL open करते हैं, तब actually एक GET request ही send होती है।
Requests Module में GET request भेजना बहुत आसान है। इसके लिए requests.get() method का use किया जाता है।
response = requests.get("https://api.example.com/data")
यह code server को request भेजता है और response variable में server का reply store हो जाता है। GET request में हम normally data read करते हैं, modify नहीं करते।
Use of GET Request
- API से data fetch करना
- Web scraping में data पढ़ना
- Server से information लेना
Exam point of view से GET request safe माना जाता है, क्योंकि यह server के data को change नहीं करता। इसलिए इसे read-only operation भी कहा जाता है।
POST Request using Requests Module
POST Request का use server को data भेजने के लिए किया जाता है। जब हम form submit करते हैं या login करते हैं, तब POST request का use होता है।
Requests Module में POST request भेजने के लिए requests.post() method का use किया जाता है। इसमें data parameter pass किया जाता है।
data = {"username": "student", "password": "1234"}
response = requests.post("https://api.example.com/login", data=data)
इस example में username और password server को भेजे जा रहे हैं। POST request sensitive data के लिए ज्यादा secure मानी जाती है।
Difference between GET and POST
| GET Request | POST Request |
|---|---|
| Data URL में visible होता है | Data URL में visible नहीं होता |
| Data retrieve करने के लिए use | Data send करने के लिए use |
| Less secure | More secure |
College exams में अक्सर GET और POST के बीच difference पूछा जाता है। इसलिए इन points को अच्छे से याद रखना जरूरी है।
Response Object in Requests Module
जब भी हम कोई request send करते हैं, server हमें एक response return करता है। Requests Module में इस response को Response Object कहा जाता है।
Response object में server से related सारी information होती है, जैसे status code, content और headers। यह concept theory और practical दोनों में important है।
response = requests.get("https://api.example.com/data")
print(response.status_code)
Status code हमें बताता है कि request successful थी या नहीं। Example के लिए 200 का मतलब request successful है।
इस तरह Requests Module in hindi को step-by-step समझना exam preparation के लिए बहुत useful होता है। Next part में हम headers, status codes, JSON response और error handling को detail में पढ़ेंगे।
HTTP Headers in Requests Module
HTTP Headers server और client के बीच extra information pass करने का काम करते हैं। Requests Module में headers का use करके हम request को ज्यादा clear और controlled बना सकते हैं।
Headers में normally information होती है जैसे Content-Type, Authorization, User-Agent आदि। Exam में यह सवाल अक्सर पूछा जाता है कि HTTP Headers का role क्या होता है।
headers = {
"User-Agent": "Mozilla/5.0",
"Content-Type": "application/json"
}
response = requests.get("https://api.example.com/data", headers=headers)
इस example में हम server को बता रहे हैं कि request किस type के client से आ रही है। Headers API communication में बहुत important role play करते हैं।
Why Headers are Important
- Authentication token pass करने के लिए
- Data format define करने के लिए
- Security increase करने के लिए
Status Codes in Requests Module
Status Codes server का response होते हैं, जो बताते हैं कि request का result क्या रहा। Requests Module में status code check करना बहुत जरूरी होता है।
Status Codes को mainly 5 categories में divide किया गया है। Exam में इन categories से related short notes पूछे जाते हैं।
| Status Code Range | Meaning |
|---|---|
| 200–299 | Success |
| 300–399 | Redirection |
| 400–499 | Client Error |
| 500–599 | Server Error |
सबसे common status code 200 होता है, जिसका मतलब request successful है। 404 का मतलब resource नहीं मिला और 500 server error को show करता है।
JSON Response Handling in Requests Module
आजकल ज्यादातर APIs JSON format में data return करती हैं। Requests Module में JSON data handle करना बहुत आसान है।
Response object में json() method होता है, जिससे JSON data directly Python dictionary में convert हो जाता है।
response = requests.get("https://api.example.com/data")
data = response.json()
print(data)
यह method exam और real-world दोनों में बहुत useful है। JSON handling data science और backend development का basic part है।
Advantages of JSON Response
- Lightweight data format
- Easy to read and write
- Python dictionary जैसा structure
Error Handling in Requests Module
Real-world applications में हर request successful नहीं होती। इसलिए error handling करना बहुत जरूरी हो जाता है।
Requests Module में error handling के लिए status code और exceptions दोनों का use किया जाता है। Exam में try-except block से related questions common होते हैं।
try:
response = requests.get("https://api.example.com/data")
response.raise_for_status()
except requests.exceptions.RequestException as e:
print("Error occurred:", e)
raise_for_status() method error status codes पर exception throw करता है। इससे program crash होने से बच जाता है।
Timeout in Requests Module
कभी-कभी server response देने में ज्यादा time ले लेता है। ऐसे cases में program hang हो सकता है।
Timeout parameter का use करके हम maximum waiting time set कर सकते हैं। यह concept performance और exam दोनों के लिए important है।
response = requests.get("https://api.example.com/data", timeout=5)
यहाँ timeout=5 का मतलब है 5 seconds तक wait किया जाएगा। अगर response नहीं मिला, तो error generate हो जाएगा।
Benefits of Timeout
- Application freeze होने से बचती है
- Better performance control
- Reliable API communication
Session Object in Requests Module
Session Object का use multiple requests में same settings reuse करने के लिए किया जाता है। यह cookies और headers को maintain करता है।
जब हमें बार-बार same server को request भेजनी होती है, तब Session Object बहुत helpful होता है। Exam में इसे advanced concept माना जाता है।
session = requests.Session()
session.headers.update({"Authorization": "Bearer token"})
response = session.get("https://api.example.com/profile")
Session use करने से performance improve होती है और code clean रहता है। Login-based systems में Session Object का use common है।
Advantages of Session Object
- Cookies automatically handle होती हैं
- Repeated headers की जरूरत नहीं
- Better speed and efficiency
इस तरह Requests Module in hindi के सभी important topics cover हो जाते हैं। यह content college exams, practicals और basic API understanding के लिए complete notes की तरह काम करता है।