REST API Overview
Explore the core concepts of REST API, including its architecture, constraints, and common HTTP methods. Understand how REST APIs facilitate interactions between client and server using JSON and HTTP status codes.
What is REST?
Representational State Transfer (REST) can be defined as an architectural style used to create and design web resources that communicate via an HTTP protocol. Over time, REST has become the industry standard in software development for creating reliable REST APIs. The REST architecture is designed to work with six guiding constraints, which are as follows:
- Simplicity
- Scalability
- Reliability
- Portability
- Modifiability
- Visibility
Note: A web resource or API that follows the above REST constraints is described as RESTful.
What is an API?
An Application Programming Interface (API) is how different systems or computers communicate. In today’s world, an API is a piece of software that can apply to almost every aspect of technology, such as web applications, command-line tools, third-party integrations, and more.
What is a REST API?
A REST API uses HTTP requests to GET, PUT, POST, UPDATE, PATCH, and DELETE data by considering the rules guiding REST constraints. REST APIs make it possible for different software to interact with each other remotely by using HTTP methods.
Features of a REST API
Some of the features of a REST API include the following:
-
It supports both JSON and XML formats.
-
It supports stateless interactions. Each request made is treated differently from the previous, making it necessary to validate each request.
-
HTTP methods are used to make requests.
-
It returns error messages that are easily understood.
Working of a REST API
Working of a REST API is relatively straightforward. A request is sent from the client-side to the server-side in the form of a web address by using HTTP request methods (GET, POST, PUT, UPDATE, and DELETE). The next step involves getting a response that comes back from the server in the form of a web resource that mostly comes in JSON.
Whenever a request is sent from the client-side to the server, we most likely contact the HTTP status code. These status codes represent an example of a public REST API and show if a request is successful or if it fails.
A simple Example of a REST API
REST APIs are made for both public and private consumption. To better understand REST APIs, we’ll briefly look at an example of a public REST API called JSONPlaceholder.
JSONPlaceholder REST API
TThe JSONPlaceholder is a free fake API for testing and prototyping, which JSON server powers. It allows us to make GET, POST, PUT, PATCH, and DELETE requests. We can see the JSONPlaceholder API in action by making a GET request to the REST API below:
https://jsonplaceholder.typicode.com/todos/1
As we can see in the terminal above, we get a response shown in the console from the JSONplaceholder server, which displays in the JSON format we have below:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
With the example above, we have successfully sent a request to a REST API and received a response from a server in return.