What is REST api?
REST (Representational State Transfer) is an architectural style for designing networked applications. A RESTful API (Application Programming Interface) is an interface that adheres to the principles of REST and allows systems to communicate over the internet. RESTful APIs are commonly used to enable communication between different software systems.
Key principles of RESTful APIs
-
Resource-Based: RESTful APIs are based on the concept of resources, which are entities that can be identified and manipulated. Resources can represent objects, data, or services.
-
Uniform Interface: RESTful APIs have a consistent and uniform interface, typically consisting of standard HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources. Each resource is uniquely identified by a URI (Uniform Resource Identifier).
-
Stateless: REST is designed to be stateless, meaning that each request from a client to a server must contain all the information needed to understand and process the request. The server should not store any information about the client's state between requests.
-
Representation: Resources in a RESTful API are represented in a format, such as JSON or XML. Clients can interact with these representations to perform actions on the resources.
-
Stateless Communication: Communication between the client and server is stateless. Each request from a client to a server must contain all the information needed to understand and process the request.
A RESTful API allows clients to interact with resources on a server in a standard and predictable way. It is commonly used in web development to enable communication between web applications and servers. For example, when you access a website and retrieve data from a server using JavaScript, you may be interacting with a RESTful API.
RESTful APIs are widely adopted due to their simplicity, scalability, and ease of use. They are commonly used in various applications, including web services, mobile app development, and Internet of Things (IoT) implementations.
Example
Let's consider a simple example of a RESTful API for managing a collection of books. This API will allow you to perform basic operations like retrieving a list of books, adding a new book, updating a book's information, and deleting a book.
1. Get List of Books (GET Request)
- Endpoint:
/api/books
- Description: Retrieve a list of all books.
- Example Response:
[
{
"id": 1,
"title": "FOURTH WING",
"author": "Rebecca Yarros"
},
{
"id": 2,
"title": "To Kill a Mockingbird",
"author": "Harper Lee"
}
]
2. Get a Single Book (GET Request)
- Endpoint:
/api/books/{id}
- Description: Retrieve details of a specific book by its ID.
- Example Response:
{
"id": 1,
"title": "The Catcher in the Rye",
"author": "J.D. Salinger",
"publication_year": 1951
}
3. Add a New Book (POST Request)
- Endpoint:
/api/books
- Description: Add a new book to the collection.
- Request Body:
{
"title": "1984",
"author": "George Orwell",
"publication_year": 1949
}
- Example Response:
{
"id": 3,
"title": "1984",
"author": "George Orwell",
"publication_year": 1949
}
4. Update a Book (PUT Request)
- Endpoint:
/api/books/{id}
- Description: Update the information of a specific book.
- Request Body:
{
"title": "The Catcher in the Rye",
"author": "Jerome David Salinger",
"publication_year": 1951
}
- Example Response:
{
"id": 1,
"title": "The Catcher in the Rye",
"author": "Jerome David Salinger",
"publication_year": 1951
}
5. Delete a Book (DELETE Request)
- Endpoint:
/api/books/{id}
- Description: Delete a specific book by its ID.
- No content in the response body.