What is REST api?

Created Jan 29, 2024Last modified Jan 29, 2024

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

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)

[
  {
    "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)

{
  "id": 1,
  "title": "The Catcher in the Rye",
  "author": "J.D. Salinger",
  "publication_year": 1951
}

3. Add a New Book (POST Request)

{
  "title": "1984",
  "author": "George Orwell",
  "publication_year": 1949
}
{
  "id": 3,
  "title": "1984",
  "author": "George Orwell",
  "publication_year": 1949
}

4. Update a Book (PUT Request)

{
  "title": "The Catcher in the Rye",
  "author": "Jerome David Salinger",
  "publication_year": 1951
}
{
  "id": 1,
  "title": "The Catcher in the Rye",
  "author": "Jerome David Salinger",
  "publication_year": 1951
}

5. Delete a Book (DELETE Request)