RESTful Principle - rahul7838/quora.clone GitHub Wiki

Let me explain the key principles of REST (Representational State Transfer) architecture.

REST is an architectural style for designing networked applications, first introduced by Roy Fielding in 2000. The main principles are:

Client-Server Separation: The client and server are independent of each other, allowing them to evolve separately. The server stores and manages data, while the client handles the user interface and user experience.

Statelessness: Each request from a client to server must contain all the information needed to understand and process the request. The server shouldn't store any client context between requests.

Cacheability: Responses must explicitly indicate whether they can be cached and for how long. Good caching helps improve scalability and performance.

Uniform Interface: The system should have a consistent way to interact with resources. This includes:

  • Resources should be uniquely identifiable (usually through URIs)
  • Resources should be manipulated through representations
  • Self-descriptive messages
  • Hypermedia as the engine of application state (HATEOAS)

Layered System: A client cannot ordinarily tell whether it's connected directly to the end server or to an intermediary. This allows for load balancing, shared caches, and security policies.

Code on Demand (Optional): Servers can temporarily extend client functionality by transferring executable code.

In practice, these principles guide how we design APIs:

  • Use HTTP methods appropriately (GET for reading, POST for creating, PUT/PATCH for updating, DELETE for removing)
  • Structure URLs around resources (nouns) rather than actions
  • Use HTTP status codes correctly to indicate outcomes
  • Provide clear, consistent data formats (typically JSON)
  • Include relevant metadata in responses