What is a REST API?



API stands for Application Programming Interface and REST stands for Representational State Transfer.. We can simply understand that how an API works, When you visit a web site and search something, It gives you the result matching to your search. Which means you have requested something and found result in response. This is how API works, it takes request, process the data and get back to you with some result which is also called response.  REST API are stateless which means we can not use sessions and cookies to recognize the client or user.
We can say an API is a set rules for building application software. It works as a mediator between user and the server. For example we need a login feature in which user can input email and password and if credentials are correct it will be logged in. So we can create an API which will take email and password as request, match these information in database and will return with appropriate result.


Request Methods

In an API there is four possible actions can be performed such as create, read, update and delete. Which is also called CRUD operation. For these operations we have different request methods.

GET - This method is used to get data or resource from the server. If a user performs get request, the server search the data and get back to the user with result.

POST - POST method is used to create new resource to the server. for example when we create a post to Instagram. A POST API called and save our post to the server which means we have created a resource to the server.

PUT AND PATCH - We have created a resource on serve but when we want to update that resource, then we have these two methods. Basically these two methods are used to update already created resource on the server.

DELETE - With this method we can delete a resource from the server. If we want to delete our post we previously created then we can use this method.

Request And Response Format

It is very important to know that in which format we can send and receive data to a REST API. JSON (Javascript Object Notation) is used for request and response in REST API. 
For example if we want to send email and password as a request, we can simply write the following JSON - 


{
    "email" : "example@domain.com",
    "password" : "12345"
}


API Authentication

We know that REST API is stateless. So what we do if we need to authenticate a user? We have the solution of this problem. To authenticate REST API we can use Tokens. Every time the client makes request, token must be exist in header, API checks for the token and authenticate that token whether it is valid or not.


Thanks for visiting. Please drop a comment if any queries or suggestions.


Post a Comment

0 Comments