🟡 Advanced Python  ·  Lesson 29

APIs और requests के साथ काम

Python API Requests क्या है?

Python API Requests ka matlab hai: APIs allow Python programs to communicate with web services and exchange data, usually in JSON format. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki fetching data from APIs jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye fetching data from APIs me kaam aata hai.
  • Ye sending form data se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
requestsrequests is an important term in this topic.
GETRequest method used to fetch data from a server.
POSTRequest method used to send data to a server.
status codeNumber returned by a server to show request result, such as 200 or 404.
JSON responseData returned from an API in JSON format.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
import requests
url = "https://api.github.com"
response = requests.get(url, timeout=10)
print("Status:", response.status_code)
print("Content type:", response.headers.get("content-type"))

Complete Example Program

Python – api-requests.py
import requests

url = "https://api.github.com"
response = requests.get(url, timeout=10)

print("Status:", response.status_code)
print("Content type:", response.headers.get("content-type"))

Expected Output

Status: 200 Content type: application/json; charset=utf-8

Program Explanation

  • import requests imports ready-made features from a module/library.
  • url = "https://api.github.com" stores a value in url.
  • response = requests.get(url, timeout=10) stores a value in response.
  • print("Status:", response.status_code) displays information or calculated result on the screen.
  • print("Content type:", response.headers.get("content-type")) displays information or calculated result on the screen.

Practical Uses

  • Fetching data from apis.
  • Sending form data.
  • Connecting python apps to web services.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Program ko api-requests.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. fetching data from APIs par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

API Requests in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.