Do you know how the REST API works and what the difference between the RESTFul API is? In this big article, we’ll take a practical look at the capabilities of this technology and learn how it works.

What is a REST API?
To make it easier for us to understand, let’s look at a small example. Suppose we have a full-fledged web application. Such an application or in other words such a website consists of two parts:

  • The client side;
  • The server side.

Everything the user sees and everything he will interact with is on the client side. Everything about the database, working with cookies and sessions, server configuration and so on is on the server side. Usually for these two things opposite technologies are used. For example, the client-side functionality is Vue JS and the server-side functionality is the Python language. Of course, instead of Vue JS and Python, you can substitute other technologies and languages that do similar things.

So, here’s the challenge. How to combine two completely different technologies so that they understand each other? This is where the REST API comes in.

Principle of treatment
For a long time programmers try to separate maximally two adjacent spheres:

  • Front-end (the external part of the project, its design);
  • Back-end (the server side of the project, its functionality).
  • You can set things up so that the user can link directly to the server through the “http” protocol. This method is widely used, but it also has a better alternative – REST API.

It is what allows us to separate the two components. Thus, Back-end and Front-end developers will not conflict with each other in any way.

Basic Actions
When a user works with a database, he has to perform several basic actions:

  • creating a record;
  • reading records;
  • updating records;
  • deleting.

These actions are additionally called CRUD (Create, Read, Update, Delete).

The essence of the REST API is that you, on the client side, address a certain URL to your project, and it in turn performs the desired action and returns an object with data in JSON format.

Example usage
Suppose we have a site called “itproger.com”. We need to get all the articles from the database. We can send a direct “http” request and mix Front-end and Back-end together. Instead of this approach, we do the following:

  • We create a new file on the server at “http://itproger.com/api/articles”;
  • In the file we write the code in the server language: Python, Java, C#, PHP or any other;
  • In the code we access the database and select all articles from the table “articles”;
  • There we convert the result into JSON format and display it on the page.

Now we can address this URL from the client side and we will get all records from the database right away. It turns out that Front-end and Back-end are separated as much as possible. All that connects them is one URL.

By addressing the address, we get a JSON object, and it is easy to fetch such an object using any Front-end technology, be it plain JavaScript or frameworks like Vue JS, React JS, Angular and others.

Other files and URLs are implemented in a similar way. For example, you can create a page “http://itproger.com/api/articles/1” to get a specific record. Any number can go instead of one and it will be a unique identifier of the record we want to get.

We can send different HTTP requests. If we send a GET request, we will get all or one record (depending on the URL). If we send a POST HTTP request, we will put a new record. If PUT – record update and if DELETE – record removal.

The difference between Rest and Restful
In fact, the two concepts are one and the same. If you create a project and you only implement the URL for fetching data, it’s a REST application, or it’s also called a REST like application.

If you implement all the server functions through REST then it is a complete REST application, or in other words a RestFul application.

There will always be the word API in the name because it means referring one project to another. When you’re working with REST, you have the Front-end side addressing the Back-end side anyway.

Leave a Reply

Your email address will not be published.

Men at the table laptop Previous post Top 10 best backend frameworks for webmasters in 2022
Monitors and laptop Next post Myths about programming