Tech N Toast®

What is REST API? How does it work?

 REST API - representational state transfer application programming interface

API - API stands for Application Programming Interface. It sends a request to a server, and the server responds by sending the requested information. It allows us to use the data that is available on some other websites. 

Example - You are going to book a flight ticket, also want to compare the flight prices. You go to one particular site, which fetches prices from different sites. There you can see the prices of different flight tickets. This becomes possible via an API, which fetches information from different websites, displays it on one website, and makes your job easier.  

==========================================


REST -  REST stands for Representational State Transfer. It is an architectural style for providing standards between different computer systems.

Let me explain what Architectural style means - It is a design concept for managing state information. Not a specific web service, but a software design pattern and a way to communicate, which is often used in the development of Web Services
RESTful web services - REST API

REST is the most important technology for web applications. Nowadays you can see that most of the major languages include frameworks to build RESTful web services. Any service based on the REST is called a RESTful service, and every RESTful service using HTTP as its underlying protocol.

We use different types of resources like pictures, video files, web pages, or other documents in our computer-based systems. A RESTful service is used to create a window for clients to access these resources. Such RESTful services are easy to implement, and easy to be maintained.

Client Computers and Servers Communicate - A Client Computer sends a request to a Server to retrieve or modify data 
(resources like pictures, videos files or any other documents), and the server sends responses in XML or JSON or PNG or HTML format.


A request is -

    1.  An HTTP verb informs what kind of operation to perform.
    2.   A header allows the client computer to pass along information about the request.
    3.   A path to a resource.
    4.   A message body containing data, which is optional.


There are 4 HTTP verbs -

   1.   GET - Retrieving resources by their IDs.
   2.   POST - Creating new resources.
   3.   PUT - Updating.
   4.   DELETE - Removing resources by their IDs.


Examples (Using IDs) - A GET request -

"GET /documents/7


Accept: text/html, application/xhtml"

In this request, you retrieve the resources by using the id 7, and the client computer accepts the content in text/html or application/xhtml.  


==========================================


REST API - Web services using REST architecture, are called RESTful APIs or REST APIs. A RESTful web service or a RESTful web API is implemented using HTTP, and the principles of REST.


REST Principles


   1.   Client-Server - Both are separate, they work independently. They are allowed to makes changes in their systems without impacting the other one. Example - A mobile application (Client Computer) will accept changes without impacting the Database in the Server.

   2.  Stateless - REST APIs are stateless. The server does not store any information about the client. The information is provided upon making a request, which means that each call has the necessary data (API key, access token, user ID, etc.) in itself.

   3.  Cache - The Client can cache the resources because REST APIs are designed to encourage the storage of cacheable data.

   4.   Uniform Interface - A Uniform Interface between the Client Computer and the Server ensures that they communicate in one language, providing a standard way of communicating.

   5.   Layered System - A system comprised of layers. The Client does not know whether it is directly connected to the server or through a proxy or other intermediary server.

   6.   Code on Demand - If the client computer requires a code (JavaScript) to be used within an application. The server can send the code via an API. 



Why do we use REST API?


Example - You want to create a web page or an app to show weather report, which is not static, but dynamic because the weather keeps changing. Now, you just want to know that how to fetch the exact data to show the reports, which are automatically updated every time the weather changes.

There are already some Dynamic web servers available to provide such dynamic data (reports).


Difference between Dynamic and Static reports -


Dynamic Reports - Such reports are live and remain active. The moment some actions take place like humidity changes or it gets colder or hotter, the reports are automatically updated.

Static Reports - Such reports require a human input, and they are not live. For example, you have inserted the humidity level – 40, it will remain 40 until you change this figure.
Web Server and SERVLET in REST API

That’s why we always need dynamic web servers to generate dynamic weather reports.

Send a request to a server to provide the data, this request is sent through an API - A passage of trust between your web page and the server.

An API creates a trust between them, and this task is accomplished by a Servlet that is created by you on the server. The Servlet accepts your request, generates the data, and sends it via the same Passage (API).  


SERVLET - It is a Java programming language class that is used in a request-response programming model.  

Your web page receives the data in JSON or XML format. Something Like this - 

JSON format -

{"Country";{
         "city";{
         "temp":55,
         "humidity":60
            }
          }
   }

==========================================
XML format -

<country>
<city>
   <temp>55</temp>
   <humidity>60</humidity>
</city>
</country>
==========================================

Lots of things are involved to generate a dynamic weather report. This process is very complex.   

In order to simplify the process, a REST API is used where you just have to create an object on a server, and that object returns you the values you request. The web server will transfer the state of an object.

That’s why it is called Representational State Transfer. Using the REST API, you are not transferring an object, but the state of an object, which can be JSON or XML or any other format. Using the REST API, you fetch the data as it is.

A REST API uses CRUD Operation to fetch data from a web server using HTTP Method.

REST API CRUD HTTP Method - POST, Get, Put, Delete
In short:

        1.  The transfer status of the action on behalf of someone is called REST.

   2.   Actual object is not transferred, just a representation of it in a form (XML or JSON or some other form).

   3.   REST is represented via some media type like XML, JSON, PNG, HTML etc.

   4.   REST is an architectural style, which means it is a concept, theory, design etc.

   5.   Data is identified by its unique endpoint, which consists of domain name with resource address.

   6.   REST APIs implementations - jersey and Sparing.



2 comments:
  1. Well written.
    Please cover about Rest end point URL and how it can be built along with which tools are used to test these APIs like postman etc can be more helpful.

    ReplyDelete
    Replies
    1. Thanks for your support. I will try to cover the recommended points in a separate post.

      Delete