Understanding APIs
REST APIs
Definition:
Stands for Representational State Transfer
- REST APIs are built on the top of HTTP, but REST is not a protocol but a set of restrictions/guidelines applied to HTTP APIs
- Commonly used and aligned with HTTP methods (GET, POST, etc.)
Key Characteristics
Stateless
- Each request from a client to a server must contain all the information needed to understand and process the request
- No Client Context Stored on the Server: Each request is independent and unrelated to previous requests.
- Examples of Stateless & Stateful (Video Listing on YouTube:)
- Stateful Example: Server tracks the user's session and knows which videos the user has already seen.
- Stateless Implementation: Client sends pagination information with each request.
- First Request:
GET /videos?offset=0&limit=10
Start from the 1st video, and fetch the next 10 videos.
- Next Request:
GET /videos?offset=10&limit=10
Start from the 11th video, and fetch the next 10 videos.
- Query Parameters:
offset and limit are used to manage pagination without storing user-specific data on the server.
- what is pagination?
- Technique to manage large sets of data by dividing them into discrete pages
- URL Parameters:
- Offset: Indicates where to start fetching the next set of data
- Limit: Indicates the number of items to fetch
- Example URL:
youtube.com/videos?offset=10&limit=10,Fetches the next 10 videos, starting from the 11th video
-
Benefits of Statelessness