This content originally appeared on DEV Community and was authored by Uzair
What the Heck is REST??? ?
REST stands for Representational State Transfer. It is nothing but some set of rules or constraints to make a web service in other words we can say its a standard way of defining a architecture.
Some Constrains that must be followed by every REST-ful Service ✅
- Client-Server Architecture: REST is composed of Client, Server and Resources (data). Request-Response is handled by HTTP requests and response, but in REST client and server must be completely independent of each other. The server shouldn't be able to modify the client application and vice-versa sever only does the job of providing a requested resource.
- Statelessness: As already said that the client must not be aware of the logic that resides in server and vice-versa. So the message sent to server from the client must contain all the necessary info about the request (Eg: Which URI is requested, Method used, etc) And thus each request data packet can be understood in isolation by the server.
- Cacheablity: Client must be able to cache server's response, this minimises the client-server request response cycle and helps to scale up the application.
- Layered Architecture: The client and server need not to connected directly there can be multiple services connecting them such as load balancer, proxies, Security Layers etc
- Code On Demand: This is an optional rule to be followed in this client has the ability to execute some code consisting of business logic which is provided by server.
-
API Rules: There are some rules which should be followed while creating endpoint for API
- an HTTP verb, which defines what kind of operation to perform
- a header, which allows the client to pass along information about the request
- an optional message body containing data
- Send a proper HTTP code to indicate a success or error status.
If any API or Service violates any of the above constraints it is not considered to be a RESTful Service ?
HTTP Verbs ??
Mostly 4 Basic HTTP verbs are used while interacting with REST Resources:
- GET : To retrieve a specific resource
- POST : To Create a new resource
- PUT : To modify an a specific resource
- DELETE : To delete a specific resource
The resource can be delivered to the client in a variety of formats including HTML, plain text, or Javascript object notation (JSON).
URI | HTTP verb | Description |
---|---|---|
api/users | GET |
Get all users |
api/users/new | GET |
Show form for adding new user |
api/users | POST |
Adds a new User |
api/users/1 | PUT |
Update a user whose id is 1 |
api/users/1/edit | GET |
Show edit form for user with id = 1 |
api/users/1 | DELETE |
Delete a user with id = 1 |
api/users/1 | GET |
Get a user with id = 1 |
How RESTful APIs work ⚙️
Let's take an example. We want to build a simple application that connects to a server and provides the functionality of creating, reading, updating, and deleting resources (also known as CRUD) within the database
A RESTful API is built and used to connect and provide communication between our client-facing front end to the back end server and database.
- When users of our application make client requests on the front end to retrieve a resource from within the database, the RESTful API is called, it makes a GET request to retrieve the resource,
- To create a resource within the database the RESTful API makes a POST request.
- To update a resource the RESTful API makes a PATCH request
- To delete a resource from within the database, the RESTful API makes a DELETE request to the Server
This content originally appeared on DEV Community and was authored by Uzair
Uzair | Sciencx (2021-09-15T15:44:32+00:00) REST APIs and Architecture – 101. Retrieved from https://www.scien.cx/2021/09/15/rest-apis-and-architecture-101/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.