What is REST and REST Principles — REST API
What is REST?
REST (Representational State Transfer) is architectural style for creating distributed applications. REST uses standard HTTP methods and principles for interaction between client and server. In RESTful API requests are executed using standard HTTP methods such as GET, POST, PUT and DELETE to work with resources.
REST Principles
REST principles are described through several key constraints that help create flexible, scalable and easy-to-maintain web services. Let's examine main REST principles:
Client-Server Architecture
REST uses client-server model, where client and server are separated and independent. Client sends requests to server, which processes them and sends responses. This separation allows server to be independent from client, and also simplifies development, updates and support.
- Client: Responsible for user interface and data work.
- Server: Processes requests from client, manages data and provides it via API.
Stateless
RESTful API is stateless, meaning each request from client to server must contain all information necessary to process request. Server doesn't store information about previous requests, and each request is considered independently.
- Example: When sending GET request server doesn't store information that user was authorized in previous request. Instead, each request must contain authorization token or other authentication data.
Cacheability
Server responses can be cacheable if necessary for performance improvement. Caching allows storing frequently requested data in memory or on disk to avoid repeated server requests.
- Example: If data doesn't change often, it can be cached on client or proxy server to speed up application work.
Uniform Interface
REST has uniform interface that standardizes interaction between client and server. This simplifies API creation and use, as client always interacts with API the same way, regardless of server's internal implementation.
Uniform interface includes:
- Using standard HTTP methods (GET, POST, PUT, DELETE).
- Resources represented as URI (Uniform Resource Identifier).
- Standardized data formats such as JSON or XML.
Layered System
RESTful API can be layered, meaning server can be divided into several layers, each performing its task. For example, one layer may handle request processing, another — authentication, third — database processing, etc. Client can interact only with outer layer, not knowing about other layers.
- Example: Using proxy servers or API gateways to separate request processing from users and database work.
Code on Demand (optional)
This principle is not always used in REST, but it allows server to send executable code, such as JavaScript, to client for execution. This allows server to change client behavior if necessary.
- Example: When server sends JavaScript code to client side for execution on page (e.g., in browser).
Resource-Oriented
In REST each object or data transmitted between client and server is a resource. Resources are identified using unique URLs. Client interacts with these resources using standard HTTP methods (GET, POST, PUT, DELETE).
REST Advantages
- Simplicity and flexibility: REST uses standard HTTP methods and widely used data formats such as JSON and XML, making it simple to use and flexible for various applications.
- Scalability: By separating client and server parts, REST API allows easy application and service scaling.
- Caching: Ability to cache server responses improves performance and reduces server load.
- Platform independence: RESTful API can be used with any platform and programming language, as it uses standard web technologies (HTTP, URI, JSON).
RESTful API Example
Imagine we have service for managing users. Some HTTP requests for this API may look like this:
GET /users— Get list of all users.GET /users/{id}— Get user information by ID.POST /users— Create new user.PUT /users/{id}— Update user information by ID.DELETE /users/{id}— Delete user by ID.