REST and APIs: Core Concepts Step-by-Step
(Hinglish Guide)
REST aur APIs ke core concepts ko samajhne ke liye, hum step-by-step har
topic ko cover karenge, jo slides mein diya gaya hai. Yeh concepts web
architecture, REST ke constraints, HTTP methods, data formats, aur OpenAPI
specification jaise topics ko include karte hain.
Step 1: Understanding Distributed Software Architecture
Kya hai yeh? Distributed systems mein, client aur server alag-alag
machines par hote hain. Communication ke liye standard protocols ki zarurat
hoti hai.
Key Points:
Servers: Data store karte hain, client ke request par data provide
karte hain, aur kabhi-kabhi computation bhi karte hain.
Clients: End users ke liye hote hain, jo data request karte hain aur
user interface ke through interact karte hain.
Network: Client aur server ko connect karta hai, data ko bina change
kiye transfer karta hai.
Challenges: Server ko client ke state ka pata nahi hota, aur client ko
server ke state ka. Network latency aur authentication bhi issues hote
hain.
Step 2: The Web and Its Limitations
Web ka Nature: Web mein client aur server physically door hote hain,
different networks aur latencies ke saath.
Key Issues:
Authentication web protocol ka core part nahi hai.
Server ko client ka state nahi pata, aur client ko server ka state nahi
pata.
Yeh stateless nature REST ke design ka base hai.
Step 3: Introduction to REST
REST Kya Hai? REST (Representational State Transfer) ek software
architecture style hai, jo Roy Fielding ne apne 2000 ke PhD thesis mein
introduce kiya tha.
Kaise Kaam Karta Hai?
REST web ke limitations ko dhyan mein rakhta hai.
Yeh guidelines ya constraints deta hai jo web applications ko scalable
aur maintainable banate hain.
Yeh koi strict rules nahi, balki ek style hai.
Step 4: REST ke Constraints
REST ke 6 major constraints hain jo iski philosophy ko define karte hain:
1. Client-Server:
Client aur server ka separation hota hai.
Server data store karta hai aur client ke request par provide karta hai.
Network data ko transfer karta hai bina kisi alteration ke.
2. Stateless:
Har request client se server tak independent hoti hai.
Server client ke previous state ko assume nahi kar sakta.
Client bhi server ke state ko assume nahi kar sakta.
3. Cacheability:
Responses cacheable hone chahiye taaki performance improve ho.
Load balancer ya proxy cache se directly response de sakte hain.
4. Uniform Interface:
Client aur server ek standard way mein interact karte hain.
Server resources expose karta hai, jo client hypertext ke through
discover kar sakta hai.
5. Layered System:
System multiple layers mein bana hota hai, jaise load balancer, auth
server, aur backend servers.
Client ko sirf next layer ke baare mein pata hota hai.
6. Code on Demand (Optional):
Server client ke functionality ko extend kar sakta hai, jaise JavaScript
ya Java applets ke through.
Step 5: REST Sequence
Kaise Chalti Hai Communication?
Client ek Resource Identifier (usually URI) se server se resource access
karta hai.
Operation (jaise GET, POST) specify kiya jata hai.
Server new resource identifier ke saath response deta hai, jo new state
ya links indicate karta hai.
Yeh process stateless hota hai, har request independent hoti hai.
Step 6: HTTP Methods aur Idempotency
HTTP Methods:
GET: Resource ka state retrieve karta hai (safe aur idempotent).
POST: Data server ko bhejta hai, resource create karne ke liye (non-
idempotent).
PUT: Resource create ya update karta hai (idempotent).
DELETE: Resource delete karta hai (idempotent).
Idempotency Kya Hai?
Idempotent operations ko multiple baar run karne se state change nahi
hota.
Example: GET hamesha safe hai, PUT aur DELETE idempotent hain,
lekin POST nahi hota.
Step 7: Data Encoding Formats
Formats:
HTML: Simple responses ke liye.
XML: Structured data ke liye.
JSON: Lightweight aur commonly used structured data format.
YAML: Documentation aur configuration ke liye popular.
JSON Example:
{
"firstName": "John",
"lastName": "Smith",
"address": [
{
"streetAddress": "21 2nd Street",
"postalCode": "10021-3100",
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
]
}
]
}
Step 8: REST API Examples
Examples:
GET /users: List of users return karta hai.
POST /users: Naya user create karta hai.
PUT /users/123: Existing user ko update karta hai.
Wikipedia API Example: Search for “Earth”:
curl "https://en.wikipedia.org/w/rest.php/v1/search/page?
q=earth&limit=1"
Step 9: OpenAPI Specification (OAS)
Kya Hai OAS?
OpenAPI ek vendor-neutral format hai HTTP-based APIs ko describe
karne ke liye.
Yeh Swagger se evolved hai, current version OAS 3.1.0 hai.
Example OAS:
openapi: 3.0.0
info:
title: Tic Tac Toe
version: 1.0.0
paths:
/board:
get:
summary: Get the whole board
responses:
200:
description: Board state
Step 10: Testing APIs
CoWin API Example: Public API to find vaccination sessions:
curl -X GET
"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPi
n?pincode=600020&date=04-08-2021" -H "accept: application/json"
Response:
{
"sessions": [
{
"center_id": 604384,
"name": "Fortis Malar Hospital",
"pincode": 600020,
"date": "04-08-2021",
"available_capacity": 85
}
]
}
Simple Full Program Example: REST API Client in Python
Yeh ek simple Python program hai jo ek REST API ko call karta hai aur
response ko print karta hai. Hum Wikipedia ke search API ka use karenge.
import requests
# API endpoint aur parameters define karo
url = "https://en.wikipedia.org/w/rest.php/v1/search/page"
params = {
"q": "Earth",
"limit": 1
}
# GET request bhejo
response = requests.get(url, params=params)
# Response ko JSON format mein parse karo
if response.status_code == 200:
data = response.json()
print("Search Results:")
for page in data["pages"]:
print(f"Title: {page['title']}")
else:
print(f"Error: {response.status_code}")
Explanation:
Line 1: requests library import kiya gaya hai.
Line 4-7: Wikipedia API ka URL aur parameters set kiye.
Line 10: GET request bheja gaya.
Line 13-18: Agar response successful hai toh result print karta hai.
Key Points (Important Highlights)
REST: Ek architectural style hai jo web ke limitations ko dhyan mein
rakhta hai.
Constraints: Client-Server, Stateless, Cacheability, Uniform Interface,
Layered System, Code on Demand.
HTTP Methods: GET, PUT, DELETE idempotent hote hain. POST non-
idempotent hota hai.
Data Formats: JSON sabse popular hai.
OpenAPI: APIs ko standard format mein describe karta hai.
Testing: curl aur Postman se APIs test kar sakte ho.
Real-world APIs: CoWin, Wikipedia jaise APIs public data ke liye
useful hain.
Idempotency: Operations jo repeat karne se state change nahi karte,
jaise GET aur PUT.