Friday, April 24, 2020

What is REST API ? | Web Service

If you have been in the Web Development you might have heard about REST API. It doesn't matter what technology you are working with it may be Java, Ruby, PHP or Python you must have heard this topic REST API.




What exactly this REST mean?  
Lets get started !!!

Let assume we are creating a Whether Reporting App (Android App). 

In order to create the app we need weather data. Hold on!!! where you will get this data? You can give static data, But it will never changed let say in Colombo 33 Celsius and Badulla 20 Celcius but weather keep changes according to the time. Other than that if you have a sensor to find weather you can find the climate that where are you at the moment, But if you need climate data around Colombo you need to fix sensors all over the area and If you need the climate data around the world you need to fix weather sensor all over the Globe πŸ™† (That will cost you Billions of Dollars πŸ’ΈπŸ’Έ)..    So we never do this. That means you need these data from somewhere.. OK from where???

Luckily we are so fortunate that we already have some web-services available in the internet. 

Let see What is web-services?




It simply means that there is a dynamic server which have all the data you need. You will request to the server and the server belongs to some company they have weather data.  And they are ready to give you that data what you have to do is just need to call them. So there is an API to do the work.
( API is something which will connect you and the server  )

How will you generate data from the server?

Let see with a java example πŸ”




You have to create a servelet that is how we generate data! and client will send you a request and the servelet will give the data. But a question can arise 
what servelet returns? It will be a html web page but here we don not need any web page just we need data. 

So the client will ask through the browser to the sever for the data and the server will give the data in two formats either XML format or JSON format. You might be thinking Why XML or JASON? because whenever returns some data, the data should have some structure 

The XML structure as following

<country>
  <city>
    <temp>32</temp>
    <humidity>40</humidity>
  </city>

</country>  

but most of the people do not like XML πŸ‘ŽπŸ‘Ž

So JSON is work worth, JSON structure as following.

{"country";{
              "city";{
               "temp":32
               "humidity":40
                       }
                 }
}

But the data return from the serverlet is overhead and bulky. To replace this we need a simple solution. That simple solution is REST API using any technology such as JAVA, Python or Ruby... 

If you want to return some data, Simply create object of that on the server side and just return the values of the object itself. Here we are sending the sate of the object not the object directly.   


We just create the object in the server side and return XML/JSON format data. That will be converting the object in to a state. That is call REpresentational State Transfer (REST). So finally you are sending the state of the object not the object that is REST.

That's all What is REST mean...

In REST API we are using HTTP Methods to achieve CRUD operation as following

  • Create - POST
  • Read - GET
  • Update - PUT
  • Delete - DELETE
That's how REST API Works... 
We will clearly talk about CRUD operation in REST in another article

I hope you like this article. Keep Sharing with other. 
Thank you 
  


imzi

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment