A REST API is an Application Programming interface which is used to interact with the RESTful Web Services and the User/Client. REST stands for REpresentational State Transfer. API is referred as a contract/intermediator between an information provider and an information user. An API helps you communicate what you want to get from that system it understands and fulfill the request by getting back a response.
Real time Example:
Think you are in a restaurant, where we are the User/Client, a Waiter who takes an order from us is an API , the chef who makes the food for our order is a Web services. Here an API (Waiter) is a mediator between the users or clients(us) and the resources or web services (Chef) who gets the order back to us what we want to get.
When a client makes a request via a RESTful API, it transfers the information/representation of the request or response from user to Webservice or vice versa through HTTP format.
REST APIs faster and more lightweight, with increased scalability—perfect for Internet of Things (IoT) and mobile app development.
The request action should be indicated by the HTTP request method. The most common methods include GET, POST, PUT, and DELETE.
· GET retrieves resources. (From the above example: Get the menu list or food)
· POST submits new data to the server.(Order new food item to your order list)
· PUT updates existing data.(Changing from one variety to another like veg to non-veg)
· DELETE removes data.(Cancelling an item if it takes more time)
We can develop REST API, programming them to send the response according to the input header of the HTTP request. There is a Media-Type attribute in the header which can be used in such cases and the response can be sent accordingly.
REST API can return both XML or JSON as response message, depending upon the Media-Type attribute in the HTTP request.
REST API must always return an appropriate Status code to the user/client so that the user/client can know the actual issue and process accordingly.
HTTP Status Code Description:
1xx: Informational: It represents the request that has been received, and it is in the continuing process. 2xx: Success: Success represents the HTTP Server response that the server has successfully received and understood the request. 3xx: Redirection: It indicates that further action must be taken to fulfil the request. 4xx: Client Error: It represents an error when the request has incorrect syntax or cannot complete the request. 5xx: Server Error: The server has failed to fulfil a valid request.