What is REST APIs?

Written by snehalnarkar9 | Published 2020/05/09
Tech Story Tags: rest | apis | programming | rest-apis | what-are-rest-apis | rest-apis-explained | rest-api-full-form | methods-in-a-rest-api

TLDR Snehal Narkar Software Engineer at www.udgama.com discusses REST API, what is the use of REST API and how it works. The API is an intermediary that allows two applications to talk with each other. API is a set of rules that developers should follow while creating their APIs. REST is an architectural style or design pattern for APIs that determines how the API looks like. API stands for Application Programming Interface and API is set of definitions and protocols for building and integrating application software.via the TL;DR App

In this blog, I am going to discuss REST API, what is Rest API, what is the use of REST API and how it works. So I hope you will get some idea about the REST API after reading this blog.

Roy Fielding defined REST . In his Ph.D dissertation in 2000, he presented the REST principles.
What is REST?
REST stands for Representational State Transfer. We can also say that REST is an architectural style or design pattern for APIs.
  • REST determines how the API looks like.
  • It is a set of rules that developers should follow while creating their APIs.
  • One of these rules says that you should be able to get a piece of data (called a resource) when you link to a specific URL.
  • Each URL is called a request For eg[www.google.com] while the data sent back to you is called a response.[it is google page that we can see on our browser]
So, before moving on to the REST API, we need to know what is an API?
API stands for Application Programming Interface.
In simple words, API is a set of definitions and protocols for building and integrating application software.
You use the APIs when using apps like Facebook, Instagram, or check the weather on your phone.
For eg.
When you visit any Food Mart, that time you provide a list of things [request in terms of HTTP] to the person working at the food mart. Then the person [API] reads the list that you give to them and finally, he provides you all the things that are mentioned in the list[that is nothing but response]. So the API is nothing but it is an intermediary that allows two applications to talk with each other. You can check the following image for a better understanding.
What is RESTful API (REST API)?
A
REST API
uses HTTP requests to GET, PUT, POST and DELETE the data.
Just for simplicity see the example below:
Suppose you’re trying to find videos about Cricket on YouTube. You open YouTube, type “Cricket” into a search field, hit enter, and you see a list of videos about cricket. In this procedure, you’re using the REST API. In a simple way, you search for something and you get the results back from the service you’re requesting from. So that is nothing but REST API when you do some request you will get data back as a response.
The example of a standard request API looks like this:
curl -v http://www.google.com
We are sending a request through curl, which is an HTTP client. Also, we can send requests by using browser utilities like Postman, REST Client, etc.
This is what we can see in the response when we request for google.com through curl:
Methodologies involved in REST API:
In order for an API to be RESTful, we need to follow these 6 constraints:
1. Uniform Interface
The uniform interface defines the interface between clients and servers.
For us this means
  • HTTP Verbs (GET,POST,PUT,DELETE)
  • URIs (resource name)
  • HTTP response (status and body)
2. Stateless
As the complete request-response process is done over HTTP protocol, it doesn’t store any data about transactions.
3. Cacheable
Many clients or users access the same server and often request the same resources, So it is necessary that these responses might be cached, to avoid unnecessary processing for the same request again and significantly increasing performance.
4. Client-Server
The uniform interface separates clients from servers. It means that clients are not concerned with backend data or data storage and servers are not concerned with the user interface or user state so that servers can be simpler and more scalable. Servers and clients can also be replaced and developed independently, as long as the interface is not altered.
5. Layered System
Between the client who requests for data, and the server who sends the response back, there can be a number of servers in the middle. These servers might provide a caching layer, security layer, a load-balancing layer, or other functionality as well. Those layers do not affect the request or the response.
6. Code-On-Demand (Optional)
The Code-On-Demand condition is optional it allows the customer to run some code on demand, that is, extend part of server logic to the client, either through scripts or applets. Thus, different customers or clients may behave in specific ways even using exactly the same services provided by the server. The code-on-demand can be used when performing some of the client-side services which are more efficient or faster.
Methods of REST API:
All of us working with the technology of the web, doing CRUD operations. When I say CRUD operations, that is nothing but we can create a resource, read a resource, update a resource and delete a resource. Now, to perform these actions, you can actually use the HTTP methods, which are nothing but the REST API Methods.
Refer below.
Status code or response code :
When you make any request you will get some HTTP status code in the response to show the status of a request.
There is a lot of HTTP status code and they are divided into 5 classes.
The first number of status code indicates which of them a code belongs to:
Status codes for 1xx:
Status coded for 2xx:
Status code for 3xx:
Status code for 4xx:
Status code for 5xx:
Why REST?
The most important feature of REST is that it enforces a uniform interface — a set of instructions or rules that everyone follows.
REST API is easy to use and flexible.
Since REST follows the standard HTTP operations, it uses verbs with specific meanings such as “get” or “delete” which avoids uncertainty.
REST API is also easy to modify and extend.
The REST API is always independent of the type of platform or language we can use anywhere.
REST is useful for building large, scalable systems or applications.
How It Works:
In any application how the rest API work is described in a diagram below:
On a website, when the client sends a request for the /users to get the list of users from the server then at that time server checks for the user's data and it sends back the lists of users as a response.
So I will show you how the REST API works. I have created one express application with MongoDB. It is a simple REST-based application, in that we can see how we can send a request to the database by using different methods[GET, PUT, POST, DELETE] and what type of response we can get.
1. GET request to the server for fetching the notes that are present in the database:
2. POST request to the server to create the note in the database :
3. PUT request to the server to update the note by using noteId:
4. Delete request to the server to delete the particular note from the database:
If you want to try out an example for REST API, you can look at my GitHub repository:
Happy coding :)

Written by snehalnarkar9 | Software Engineer at www.udgama.com
Published by HackerNoon on 2020/05/09