In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined by REST. Further, we will understand different methods for CRUD operation.
It is a set of guidelines that allows programs to interact with each other. API is created by developers on a server and permits the client to interrelate with it.
The Representational State Transfer is the expanded name of REST. It takes HTTP requests into account for communication with web services. It uses numerous values such as HTTP, JSON, URL, and XML for data communication and transfer. It is a set of protocols that developers keep in mind while dealing with the creation of API.
A client sends a request in the form of a web URL in the form of HTTP GET or HTTP POST or HTTP PUT or HTTP DELETE request. Further, an answer is received from the server in the form of resources which may be either HTML, XML, Image, or JSON. JSON is the most commonly used resource among all.
Boundaries on Architecture as defined by the REST:
- Client-server Architecture: The Client and Server architecture is necessary for a REST application. The client is responsible for requesting resources and the server is responsible for storing the resources. Neither of them depends on each other for anything. They function independently.
- Cacheable: It is important for every response to include whether the reply is cacheable or not and the time for which the response can be cacheable on the client-side. The client will direct the data from its cache for any following request without the need of sending the data again to the server.
- Standard Interface: This interface enables the client to interact with the server in a single language that is consistent and standardized irrespective of the architectural assistance.
- Stateless: It is the request which is capable of storing the important states to handle the requests. While the server stores nothing as such.
- Layered System: An application architecture comprises several layers. Each layer is independent of the others but the intermediate layer. The intermediate layer plays a key role in improving the readiness of the system.
- Code on Demand: It is a discretionary feature. Here, the server is responsible for providing executable code to the client.
There is five CRUD (Create, Read, Update and Delete) methods popularly used in REST based architecture:
GET: The GET method is employed to read or retrieve an illustration of a resource. It returns an illustration in the format XML or JSON.
POST : The POST verb is most often employed to create new resources. To be precise, It is used to create subsidiary resources.
NOTE: It is easy for the users to use GET and POST methods without the need to install any methods override.
PUT: It is employed to update the aptitudes. It is preferred when the user chooses to update or replace all the existing resources in a go. Though, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server.
DELETE: It is employed to delete a resource identified by a URI. The unique key of the object is passed to delete a key.
NOTE: To use PUT and DELETE methods, the user needs to install methods override which can be done in the following way.
npm install method-override –save
The following package needs to be installed before being able to use the above.
var methodOverride = require("method-override");
PATCH: It is mainly used to update a request on the server just like PUT. But the difference is it can be employed to delete patches or part of data and not the whole data in a go. The PATCH request only requires to comprise the changes to the resource, not the complete resource.
Now, let us understand endpoints with the aid of a table.
URI HTTP Explanation api/users GET Employed to fetch all the users api/users POST Employed to add a user api/users/2 PUT Employed to update a user with id = 2 api/users/2/edit GET It shows the edited form of the user with id = 2 api/users/2 DELETE Employed to delete a user with id = 2 api/users/2 GET Get a user with id = 2
NOTE: To keep an API URI (Universal Resource Identifier) consistent, it is needed to use plurals in the URL.