What Is REST API? For Beginners
- admin
- 0
- on May 31, 2022
Hey guys in this articles i’m going to try to explain you what is rest and what is our rest api so let’s get started so first of all what is rest rest is an architectural style which is based on web standards and the http protocol this style was initially described by roy fielding in the year 2000 roy fielding was also one of the principal author of http specifications now rest is an acronym standing for representational state transfer so as i said that rest is an architectural style of web but what does this even mean so rest is a set of design criteria and not the physical structure or architecture of the system so it’s just a design
criteria right so rest is not tied to web that is it doesn’t depend on the mechanics of http now in rest based architecture everything is a resource and what is a resource a rest resource is a data on which we want to perform operations so this data can be present on the database on the server side for example some employee data which have some unique identification for example employee id now this resource is accessed via a common interface based on http standard methods which we will learn a little bit later so let me give you a more concrete example so in rest based architecture you have a rest server which
provides the access to the resource so for example this is our rest server which provides the access to the resource which is there in the database or in some form of records now on the other hand we have a rest client which is used to access and modify the rest resource now in rest architecture every resource should support the http common operation so let me give you a more concrete example so let’s say we want to create a weather app now weather app is dependent on the weather data now let’s say this weather data is
saved every day or every hour in some database which can be accessed from a rest server for example so in a rest based architecture you have a rest server which provides an access to the resource in our case it’s a weather data of some city for example and you have a rest client rest client can access or modify the rest resource okay now every resource should
support the http common operation now how this resource is identified so resources are identified by a global id which are typically called uris which stands for uniform resource identifier and in some cases it’s called url which stands for uniform resource locator so you can consider this uri as an address of the resource so for example we want to access the temperature of city London now this request can be sent from a client in the form of uri you can see here so for example first of all this resource is under the country name us for example and then the resource name London now if we think logically a resource must have at least one uri right so if the resource doesn’t have any Uri how will you access this resource so in rest architecture you must have a Uri to access or modify this report for example if you don’t have this uri uk forward
slash London how will you identify this resource in the rest server and these uri should be descriptive that is human possible and have some structure so for example in our example i have given this uri uk which is a country name and then the city name for example now if you want to know the temperature of some other city in some other country for example we want to know the temperature of city Munich then you can write Germany as a country here and then forward slash you can write the city name which is Munich in our case now rest allows the resource to have different representation right so for example this request is sent to a rest server and on the request this rest server accesses the London’s temperature report and then how or in which representation it should send the result and in which format this data is returned to the client is called a representation right and rest allows that resource to have different representation
it can send an xml representation of the result or it can send json representation of the result or in most of the cases it can also send html as a representation now in rest clients decide which kind of representation it want to have so for example client sends this information that i want this result in the representation of jason right so client send this request to the server that which form of its presentation of result it wants so let’s go to the rest definition once again so why rest is called representational state transfer
so representational state transfer refers to transferring representation in our case we have seen the representation can be sent in the form of json or text or html or xml in any representation so we are using the representation of resource to transfer this resource state which lives on the server into the application state which is on the client okay so representational because client possesses the information necessary to identify modify or delete a web resource and client decide which form
of representation of the result now the word state comes in rest because all resource state information is stored on the client side so you can say that your server or your rest server is stateless it doesn’t have any state all the states are stored on the client side and transfer because client state is passed from the client to the server through http now let’s discuss about http methods supported by rest so these are some of the most important methods which are supported by rest architecture so you have get which is used to retrieve representation of resource you have put method to update or modify existing resources and in some special cases we use put to create a new resource also now we have post to create a new resource and delete method to delete an existing resource now rest have to another less commonly used methods which are had an option so had method fetches the metadata of representation only so it doesn’t have any body and the method options checks which http method a particular resource supports so most of the time when you are designing your rest architecture
you will be using these four method which are frequently used when you design your rest architecture which are get put post and delete now there are five basic software architectural style principle in rest so the principle one is everything is a resource so in rest architectural style data and functionality are considered as resource and are accessed using a uri now the second principle is every resource is identified by a unique identifier using Uris now the third principle is you have to use simple and uniform interfaces so use
simple and standard interfaces to define your rest architecture so for example in our temperature or weather information example we have defined a very simple and uniform interface so we have defined a country name and then inside a country name there is a city name so whenever we provide the country name and then a city name and for example we want to access the temperature of some particular area of that city we can even request that temperature using this Uri so use simple and standard interfaces for defining your
rest architecture now the fourth principle is communication is done by representation so for example rest server can send a representation in the form of xml or json or text or any other form on the client’s request and the last principle is be stateless that means every request happens in complete isolation now the next question arises is what is a rest api so first of all we need to understand what is api so api stands for application programming interface and an api is a set of subroutine definitions protocols and tools for building application software now our rest api
is an application program interface that uses http request to get put post and delete some resources now let me give you a simple example to explain a rest api so let’s say we have a resource for example some dishes inside a kitchen so these can be considered as resources right now a client comes and he wants to order some dishes from the kitchen but he cannot directly go to the kitchen and then take whatever he wants so the user uses some intermediate service to access
this resource and this is the job of an api which is waiter in our case so a rest api accesses this resource on the client’s request and then responds this representation of the result to the client so we can say that our rest api is a intermediate service to access some resources now a real world example of that can be a twitter api or a google map api or for example Facebook’s graph apis so for example we want to make an app which uses for example Facebook’s graph api to know who have the most popular page on Facebook you can use facebook’s graph api for that or for example if you want to post something on twitter
using the twitter’s rest api you can do that also so let me give you this real world example so open your browser and search for api g console okay so search for api g console and you will be able to see this website which is api key dot com forward slash console so when you go inside this website you will be able to see different apis here okay and you will be able to see for example fakebook’s api yahoo weather api and so many different kind of apis so for example you want to use a twitter api which is already selected here or you can even select it from here so for example let’s say i want to do a tweet on my twitter account using this api how can i do it so i will select this twitter api and you can see this is the service which is api.twitter.com version 1.1 and i need to give some so i need to give some credentials from my site to login to my twitter
account and then only i will be able to do some tweet right so for authentication i will choose one here so just choose this option out one and then just select sign in with twitter it’s going to ask for your permission i’ll just say authorize app and now this app is logged in with my credentials right and now i can do some tweet and on the left hand side i will search for the tweets section here and then i will just select this option which says status is update dot
json which uses this http post method right so i will select this option from here which is going to give you this kind of url so you can see the resource we want to access here is statuses and then update dot json and this is uh the status we want to post so for example here from here i’m going to just delete the status which is the default status and i will just write here rest stands for representational state transfer and now i’m going to just send this tweet and i receive the status code 200 that means my tweet has been tweeted and you can see this response here right and i can see here that this tweet is posted on my twitter account you can see rest stands
for representational state transfer which i have done using this twitter’s rest api now the last example i want to give here is the rest server i created using node.js so this is a very basic rest server which is used to create update delete or get the blog post so a blog can have post and then the post can have some comments and you can have also a profile of the person who is posting this blog post right now this is a rest server and i need to have a rest client in order to interact with this rest server so generally in development we use a tool
called postman rest client which is used to send rest based request from a client to a server so open your web browser and search for postman rest client and the first link which will appear here will be from getpostman.com and then you can directly download this for your operating system and once you download and install it will look like this okay so this is a simple user interface of postman client from here you can send any request to your server so my server is running on local host port 3000 so i’m going to just write here http colon double forward slash local host port 3000 and for example i want to just send the
get request from here and let’s see on the server side i’m using this db.json as my database so for example you can see there is only one post existing in this json database and i want to access this post resource right so what i can do here is i can just send a request on post so this is my resource posts is my resource and let’s say i want to just get the post whose id is one so i’m going to just write one here right and then i will just send this request and then you can see i get this response so for example id is equal to one title is json
server and author is code bind which is also there on my json database now let’s say i want to just retrieve the comments for post one then i can just write instead of post i can just access the comment resource from here right so i can just send this request and this time i can just get the comments resources from this rest server now let’s say i want to post a new post so i will just send this request so i will just use this resource post to post some data and the method here i will use is post because i want to create a new blog post and
format of this will be same as this one so i’m going to just copy this format and then in the body i’m going to send this request so just click on body and then i will just say bulk edit and i will send this request so this time the id of the post will be 2 the title list will be for example rest api and the author i want to leave it as same so i will leave it as code bind so this is my request body for the post right and then in the header you can even write the content type but by default the postman sends the application j-son content type to the rest server so i will leave it as default and i will just say send and you can see here the status i get here is 201 created that means my post is successful and i receive the id of the created post now in order to delete the post i can choose the delete
method from here and whatever post i want to delete so for example i will just write resource name posts and i want to delete the post 2 so i will write the id of the post 2 and then send the request and then you can see i get 200 response that means ok and then this id is deleted and then when i do the get request for the post id 2 i will not receive any data about this and when i request all the posts then i will only get the post id 1 because post id 2 i have already deleted so that’s it for this articles i hope you enjoyed this articles