Codementor Events

What is a Rest API. . .?

Published Jan 16, 2021Last updated Jan 17, 2021
What is a Rest API. . .?

About me

I am full stack java developer and a curious learner. ⚑️

πŸ”₯ What is an API?

Application programming interface is a way to interact with other programs or softwares.it can be designed using custom or the provided standard set of rules.

E.g. πŸ• Zomato API's can be used to fetch restaurant details by cuisine.

What is a Rest?

Rest is an architectural style using which two programs or applications can interact with each other over HTTP protocol.It uses HTTP protocol to send requests and receive responses.it is as similar as trying to access a web api through a browser over HTTP.API's which follows rest architecture guidelines are called REST API's.We can build a rest api on server and expose it so that client can access it via another application or a browser. most of the time REST API's are useful to access a resources (data) from a server.
most of the time the REST Api's renders the response in JSON format which can be used to populate the views in client applications.

  • Rest has high performance πŸš€ and more scalability than other web services.
  • Rest supports many resource representations like HTML,Json and XML.
  • it can be cached
  • We can use SSL capabilities with HTTP for more security.

E.g. Weather API's provide a json data which can be useful to show weather data on many applications.

rest2.png

Components of Rest API

1)The Endpoint URL
2)The method
3)The Body
4)The Header

Let's try to understand each term.

The Endpoint URL

it is URL which server provides to client to access the data or resource. Github provides https://api.github.com using which we can retrieve our github repositories.

We can access our pushed repositories from github using below API url:

https://api.github.com/users/<github_username>/repos?sort=pushed

this will fectch all the repository data from our github account in a sorted manner.

The Method

The method is a type of request client wants to send to the server. it can be any of the below methods

1)GET( it is used to retrieve the records from server)
2)POST (it is used to create a record on server)
3)PUT (it is used to create/update the record on server)
4)DELETE( it is to delete the record on server)
5)PATCH ( it is used to update the partial record on server)

GET request can be cached so it should not be used for sensitive data retrieval like bank account details.

the difference between POST and PUT is PUT request is idempotent(means when trying to call same PUT request again and again will product the same result)
but in case of POST it is not idempotent.

We can see in below image where GET method is used to access the github repository data using API.

get.PNG

The Body

The body contains the data which client wants to send to the server.
we can send the request body from client to server to add a below record in server in json format(probably nosql mongodb or relational db).

{
"name":"Vishal",
"id": 1
}

or server can send the response body contains the confirmation about the record creation.it may be success or error information.

{
error:"record cannot be created due to duplicate id"
}

The Header

The Headers are used to provide some information to server about client or vice versa it may be for type of data, authentication etc. in above image we can see the header information where one of the property is content-type :application/json.
req.png

the header can have information about what will be in the request body or important information about authentication/security.
the headers are key value pairs where content-type is a property and application/json is a value.

We can fetch the Rest API response using below python code which uses requests(python http library).

**import requests

#  Endpoint URL
url = "https://api.github.com/users/<github_username>/repos?sort=pushed"

response = requests.get(url)

print(response.txt)**

πŸ‘‹ Thanks for reading and feel free to provide your feedbacks.
In upcoming posts We will be learning how to create an REST api using java in Spring boot and python with flask framework .

Discover and read more posts from Vishal Bhosale
get started
post comments3Replies
Bristol Parks
8 months ago

Thank you so much for sharing. I was actually looking for this https://stateofwriting.com/uk/literature-review-writing-service website for my younger sister who is not so good at writing an essay assignment and because of this, she asked me to help her in searching for a site online through which she can hire essay writer easily. So, I started searching for it online on google search and I found that website and also I found your post link. I love Lamborghini and that is why I came here to appreciate you for sharing that picture with us.

HenriBoehm
9 months ago

Thanks for sharing it with us, I appreciate you.

Sumit roy
4 years ago

Well explained the rest web service…