What is REST API?
REST API (Representational State Transfer Application Programming Interface) is a web service architecture. This architecture encompasses a set of standards that represent resources and define operations performed on these resources. REST typically communicates over the HTTP protocol but can also be used with other protocols.
Key Principles of REST
Resource Representation
Each resource is represented by a URI (Uniform Resource Identifier). These resources are typically data represented in JSON or XML format.
Lightweight Communication
REST communicates using existing communication protocols like HTTP, contributing to the overall lightweight nature of the application.
Statelessness
Each request contains its context, and the server does not store this context. Every request must carry all the relevant information with it.
Layered Architecture
REST follows a layered architecture, making the application modular and more sustainable.
Interconnected Systems
RESTful services can interact with each other, enabling access to a resource in one system through other interconnected systems.
CRUD Operations
REST APIs often support CRUD (Create, Read, Update, Delete) operations. For example, in a book store application:
- Create: Use a POST request to add a new book
- Read: Use a GET request to view specific book details
- Update: Use a PUT or PATCH request to update book information
- Delete: Use a DELETE request to remove a book
Commonly Used Status Codes
| Code | Meaning | Description |
|---|---|---|
| 200 | OK | The request was successfully processed |
| 201 | Created | The resource was successfully created |
| 400 | Bad Request | The request could not be understood |
| 401 | Unauthorized | Authentication failed |
| 404 | Not Found | The requested resource could not be found |
| 500 | Internal Server Error | An unexpected error occurred |
REST APIs can be utilized for a wide range of applications and were developed to standardize the interaction of web-based services.