In this tutorial, we are going to learn about REST and HTTP. REST stands for Representational State Transfer is an architectural pattern for developing APIs.
Before starting with REST, first of all, we need to know about APIs and why we need APIs.
What is API?
API is nothing but an Online Web Service that exists on your web server and can be consumed by many clients. Suppose I have an application that may have several mobile apps on the different platform and of course some kind of web application too.
Why we need API?
If I am not using an API my application structure may look like this where each client app will have its own business logic. See the below image.
From the above image, you can understand that each client application has its own business logic most probably it can be written in different languages. These client apps are directly connected to the database to manipulate data. Now the problem that may arise:
- The application becomes complex as it grows.
- Each Client application needs to be maintained in sync with each other.
- When a new feature is added or updated all client application need to be updated accordingly.
- To maintain each client application separately can be an expensive process.
Now consider the following approach:
In the above approach, we have a Central API on our web server. Each Client uses the same API to manipulate data. If you want to make any change to business logic then you just need to make a change to central API. The client applications will get updated accordingly.
What is Rest?
REST stands for Representational State Transfer was first conceived by Roy Fielding. REST is an architectural pattern that uses HTTP as its underlying pattern. It is an approach to communications that can be used while developing Web Services. REST is lightweight as compared to SOAP (Simple Object Access Protocol) because it uses less bandwidth which makes it a better option to use over the internet.
When a Web Service uses REST as an architectural pattern then it is called RESTful APIs or simply REST Services.
REST uses HTTP as its base protocol. HTTP is a stateless protocol in which a client sends a request to the endpoint and the endpoints respond. The term “Stateless” means it does know about the previous request. There are several key terms related to HTTP.
A resource is the URL or Address where your REST API exists.
for example, http://somdomain.com/api/employee is a resource.
HTTP uses set of Request methods to perform the desired action on given Resource.
Get Method is used to retrieve data.
The HEAD method is similar to GET except that the server MUST NOT return a message-body in the response.
The POST method is used to submit data to the specified resource,
The PUT method is used to update data to the specified resource.
The DELETE method deletes the specified resource.
The CONNECT method establishes a tunnel to the server identified by the target resource.
The OPTIONS method is used to describe the communication options for the target resource.
The TRACE method performs a message loop-back test along the path to the target resource.
The PATCH method is used to apply partial modifications to a resource.
A header is used to send additional information. You can send information like what type of information you want from the server, Authorization details.
Request Body is used to send data along with Request.
This is the main body of the response. This might be JSON, XML or HTML.
Response Status Codes:
HTTP defines standard status codes that can be used to express the results of a client’s request.
These Status Codes are divided into five categories.
1- 1xx: Informational:
Communicates transfer protocol-level information.
2- 2xx: Success:
This specifies that the client’s request was accepted successfully.
3- 3xx: Redirection:
This specifies that the client must take some additional action in order to complete their request.
4- 4xx: Client Error: This Status Code returns if any client error
This Status Code returns if any client error occurs(for example invalid request).
5: 5xx: Server Error:
This Status returns if any server error occurs.
Example of GET Request.
List of Employees
Example of POST request.
Create New Employee.
There are six major Constraint associated with REST
This Constraint simply decouples the architecture evolve independently. This Constraint act as an interface between client and server.
This Constraint specifies that server must not hold any information about the state of the client. The Information Status must hold on Client Side such as Session.
Since many clients access the same server and same resource so it is important to cache the responses to avoid unnecessary processing and increase performance.
Since Uniform interface separates the client from the server. This separation specifies that client does not need to know about data storage which remains internal to the server. This improves the client code. On the other hand, the server does not need to concern with User Interface or User State. So the server can be simpler and more scalable
This Constraint specifies that we can use Layered system architecture with Rest. This means that we can deploy the API on the different server, Store data on the different server and authenticate request on the different server. A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way.
Code on Demand:
This constraint specifies that server can extend or customize the client functionality by transferring logic to it. For Example Java applets.
All these constraints will help you to create a RESTful API.
Hope you loved my this tutorial about REST and HTTP. I would appreciate your feedback, Comments and suggestions.