While learning about Flask and how to build APIs, I have recently heard/read this phrase a lot. Initially I was more focused on actually learning Flask and how I can use that to build an application. But now that I am past the beginning part of learning Flask, what does RESTful actually mean? Let's take a look at what this means and why it matters
What is RESTful?
This isn't some library, function, variable, or anything like that. REST is a set of design principles, or guidelines, for creating APIs that were made by Roy Fielding. REST stands for REpresentational State Transfer. So when the API is being called to, the server will transfer a representation of the state of the requested resource to the client.
What are these Design Principles?
Uniform Interface
Uniform interface has a few components to it, but for simplicity's sake we will try to keep it short. When a request is made to an API from either an app on an android device or a Safari web browser, the request should look the same. Also, the resource being sent back to the client should have all of the information the client will need, but also should not be too big.Client-Server Separation
Client-Server separation is a little more straightforward. The only way the client should interact with the server is with the URI of the requested resource. The only way the server should interact back with the client is by passing the requested data back. Changing code on either the front-end or the back-end should not affect the other side at all. Unless of course you get rid of the request to the server.Stateless
Stateless refers to the how the server is not able to store any information that is related to a client request. So every time a client makes a request, it needs to do so with all of the necessary information to process.Cacheable
Cacheable is referring to the possibility of the client saving the requested information so that another request does not have to be needlessly made. The information should have some form of version attached to it so the client can check to see if the information needs to be updated.Layered System
Layered System refers to how there may not be direct communication between the server sending the information and the client requesting it but the server and client should not know if they are communicating directly, if there is a layer in between them, or if there are multiple layers between them.Code on Demand
Code on demand is deemed to be optional, but it means the ability for a server to send executable code to the client.
Why does it Matter if your API is RESTful?
The whole purpose of these design principles is to help build a good API, but that doesn't really answer how these design principles help make a good API.
Flexibility
Both guidelines, layered system and client-server separation help make a flexible API. If the code on the server side does not affect the code on the client side, then you can freely make changes to the code on the server side without causing any issues. The same can be said if the code on the client side, does not affect the code on the server side. Also, if the API is already setup to account for an unknown amount of layers to get to the client requesting the information, then there should be no issues if changes need to be made there as well.
Independence
APIs considered RESTful won't care what language the client is requesting the information in. The design will be unaffected by the variability here.
Scalability
When an API is considered RESTful, they are much more efficient at scaling. One way this can be seen is statelessness. Since the server does not have to keep any previous client request information, it helps remove some server load. If the server does a good job of labeling what is cacheable and has versions attached, then it is possible the client won't have to make as many requests and that will also help lighten the server load.
Brief Conclusion
So yes, an API considered to be RESTful has a lot of benefits. There are even more benefits than the ones I listed above. When you start to learn something new, it is easy to gloss over certain things that don't seem important at first or maybe there is just initially some information overload so some things get left behind. That is why it is can be helpful to look back and see what information was initially presented when learning a new topic.
Top comments (1)
Thank you Jeffrey for this blog. I got a better understanding of restful.