The term REST was coined in 2000 by Roy Fielding, one of the creators of the HTTP protocol. Today the REST API is used everywhere to transfer data between the client and the server.
So, what is a Restful API and what are its advantages? REST API is a composite of two acronyms:
API is an Application Programming Interface; this is an interface used to make one program work with another.
REST is Representational State Transfer, a way to build an API architecture using HTTP.
The REST API is responsible for the interactions between the server and the client application. In simple terms, the REST API is used whenever a website or application user needs to get resources from the server.
HTTP is the basis of REST services because this protocol is implemented in all kinds of programming languages, as well as all operating systems, and is universal. With HTTP, it is possible to provide a user with a specific resource using its URI.
A resource is something that needs to be shown to the client. For example, we created an application with lists and descriptions of cafes in the country. A resource can be a list of all the cities in the application.
Resource representations consist of data; metadata describing the data, and hypermedia links to the next desired state.
The URI is a universal resource identifier. For example, http://allcafes.com/cities, where the first part (http://allcafes.com) is the site or server address and the second (/cities) is the address of the resource on the remote server.
There is no specific standard for the REST API, but there are several principles to keep in mind, particularly in creating interface code.
The benefits of the REST API and the popularity of its use are based on the principles of its work.
Working with the REST API, you get scalability. This is possible thanks to the principle of separating the client and the server.
The flexibility of the REST API is ensured due to the use of HTTP and the ability to work with any programming language, in any operating system. The created API architecture enables handling of any type of request and data format.
Using the cache can improve performance because it will reduce the number of requests to be processed.
Reliability and security in REST APIs come from the layered system. It allows you to stop attacks at different levels, preventing them from penetrating the entire server architecture. For example, critical or most vulnerable elements of the API architecture can be stored behind the firewall.
Despite these advantages, SOAP, GraphQL, and RPC are trying to take REST's place. SOAP, invented before REST, and RPC, which has several versions, are more prevalent in narrow areas. But GraphQL is considered to represent good progress in this area for more complex projects.
The operation of the RESTful API is similar to that of any website on the Internet. A user (client) communicates with the server using the API when he needs some web resource.
Step by step, the work of RESTful API can be described as follows:
HTTP commands are used to work with resources through the REST API:
After the completion or failure of any command, the user receives a response beginning with a certain number. For example, a code starting with 2 means the successful performance of the command, 500 - server error, and codes with the number 4 in the beginning indicate the cause of the error (401 Unauthorized - unsuccessful authorization, 404 Not found - requested a non-existent resource, etc.).
The OpenAPI specification, or REST API development best practices, allows any developer to understand API architecture parameters and capabilities. Specialists share their experiences on the Internet.
Bad: /cafeKahve or /cafe_Kahve
Good: /cafe-Kahve
Bad: DELETE /city or DELETE /City
Good: DELETE /cities
Bad: GET /cafe/:cafeId/group/:groupId/price
Good: GET /cafes/:cafeId/ or GET /group/:groupId
Bad: POST /updatecafe/{cafeId} or GET /getcafes
Good: PUT /cafes/{cafeId}
For example, API Blueprint and Swagger.
Use HTTPS for all resources and services.
We recommend using standard HTTP status codes. HTTP codes make it easier to know what caused the error.
The API database can become quite large and without proper sorting, processing and finding the right resource will take a long time.
More REST API practices can be found here.