REST stands for Representational State Transfer. It is an Architectural Style which follows certain principles.
Fundamentals of REST
- Resource
Everything in the REST is a Resource, like Images, Text, Web Page, Videos, File,
Some examples of Resource with URI,
www.example.com/Images/image.jpg (Image Resource)
www.example.com/Video/001 (Video Resource)
www.example.com/home.html (Web Page Resource)
www.example.com/PDF/data.pdf (File Resource)
In REST start thinking about the resource instead of data, Physical files and SQL etc.
- Unique Identifier
In REST every resource is identified by a Unique Identifier.
In old web resource is identified using the URL,
For Ex:
If you want to display the data of employee we will use www.example.com/EmployeeDetails.aspx
In REST every URI uniquely represents a single Resource
For Ex:
i. If you want to display the data of employee whose EmployeeId is 1 then URI should be www.example.com/Employee/1
ii. If you want to display the data of employee whose name is Peter then URI should be www.example.com/Employee/Pruthwiraj
Here, rather than a pointing to the Generic page, in REST we actually point to the Unique Identifier. By using that identifier we can easily identify that resource.
- Unique Interfaces
Use simple unique interface instead of complex and duplicate interfaces.
Suppose, if we are performing the CRUD operation for employee details, we are use the methods names like InsertEmployee(), UpdateEmployee(), DeleteEmployee(), GetEmployee(). So these method names are inconsistent and difficult to remember.
But REST says keep your interfaces uniform and simple and this can achieved by using the HTTP methods and combining the name of your resource operations.
For Ex:
Get | Get a resource |
Put | Update a resource |
Delete | Delete a resource |
Post | Submit data the resource |
Compare these HTTP methods with our CRUD operations method name and create Uniform URI,
Normal method names | HTTP methods | Uniform URI |
GetEmployee() | Get | www.example.com/Employee/ Pruthwiraj |
UpdateEmployee() | Put | www.example.com/Employee/1 |
DeleteEmployee() | Delete | www.example.com/Employee/5 |
InsertEmployee() | Post | www.example.com/Employee/Pruthwiraj |
We will creates the uniform interfaces by combining the HTTP methods and Resource Name.
- Representation
All communication is done via Representation. Whenever the client sends any data to the server and server sends the response as a resource. This everything is done via Representation.
For Ex:
Suppose, when we are creating the employee and send the below request to the server
- <Employee>
- <Name>Pruthwiraj Jagadale</Name>
- <DOB>23/01/1989</DOB>
- <Gender>M</Gender>
- <Designation>Programmer</Designation>
- </Employee>
And the response coming from the server is,
- <Employee>
- <EmployeeId>1001</EmployeeId>
- <Message>Employee created successfully.
- </>
- </Employee>
The above representation is in to the XML format, you can also use the JSON format.
Representation means all the request and response which takes place between client and server is nothing but the Representations.
- Stateless
Every request should be an independent request. Independent request means with data you also send the state of the request so that the server can carry forward that state for the next level of request.
REST is an Architectural Style which used the HTTP methods and follows some principles. i.e. Resource, Unique Identifier, Unique Interfaces, Representation and Stateless.
Read more articles on REST: