hello and welcome to this session on rest web services today we are going to learn some of the principles of rest web services this session is going to be a continuation from our last session where we looked at rest and we understood about restful web services and we started with the principles of rest so let us begin in our last session we have learned about uniform interface and we looked at what is a resource what is a URI and how do we make use of HTTP methods today we will look at statelessness what is caching
what is layering and what is code on demand so let us begin with statelessness and in a restful architecture we say that all the request and response or all the communication between client and server are stateless and this means that the server does not maintain any state of the system and the client has to send a request which is complete in itself so the client sends a request which is independent and it does not depend on any of the previous request let us understand this so statelessness says that server should not require to store the
state of the session and it means that the client has to send a request which is complete and independent and a complete entity in itself let us take an example if you are doing an online shopping and you are adding items to your cart now the server will not maintain the state of your card so every time you are sending a request to the server it should also include the current state of your card and how does this happen we will look into the next concept which is caching so for statelessness because the server does
not have the overhead of storing or maintaining the session therefore it improves improves the web services performance now let us go to caching in the last concept we saw that the server does not maintain the state of the session so in that case how do are the two entities the client in the server communicate when we have to coordinate with each other and for example there's a login scenario where the server has to know whether the authentication is successful or not and similar scenarios so that happens with caching and caching happens at the client side
so whenever a client sends a request to the server the server gives back the response which contains the actual data and along with the actual data there are some other metadata or there are some other information in the headers which tells the client whether it has to store the response locally or not or in other words whether the client has to cash the response or not so the the server generates a response and in the response we also have some headers like cache control and last modified which tells the client whether the response should be
cached or not let us see an example of a response so this is a response from the server and if you look at the headers we can see that there is a cache control header which tells that what is the maximum time that this response should be cached and then you can also see an eag here right so eag is is entity tag and uh every unique version of the resource has a unique entity tag so if the resource changes there will be a new entity tag given to that particular representation so with this we
can do caching so the client uses the cache control header to determine whether the resource or the response has to be cached or not so with this uh principle it helps coordinate between client and server and maintain a coordinated communication and this is how the server can remain stateless so this was the concept of caching let us understand layering now when there is this communication between client and the server rest principle says that there can be multiple layers in between and these layers can be HTTP intermediaries and they can be used for a number of
purposes they can be used for message translation improving performance caching Etc so we can also have layers like proxies and gateways so proxies we can have for evaluating the request or simplifying the request and gateways we can have for managing the traffic at the server end and so on we can also have some layers which can help us in caching so for example we have a layer which helps in caching and stores the response for an hour and if we get a request from the client within that hour and there is no change in the
resource then we can get the response back from this particular layer and there's no need to go to the server so it improves performance and scalability so uh layering improves the scalability and now we can understand exactly how layering works and what is the concept of layering the next concept is code on demand and it is an OP optional constraint of rest web services so it is something like when you send a request to get back a response the response has the ability to run some code or some JavaScript on the client side so again
it extends the functionality of the client so this is an optional constraint of rest I hope now we can understand what is representational State transfer means so this means that every resource can be represented in multiple ways so a resource may have a representation in an XML format or a Json format and whenever there is a request from the client the representation of the current state of the resource is being transferred between client and server let us look at an example suppose I do a get on the images resource and I want to retrieve the
image one and the representation I requesting is jpg so I will get a response with the jpg representation of the resource now if I again do a request and I do a get on the images resource and I want to get that details of image one but this time I want a representation in XML I will get a response with the XML representation of the resource so now we can understand representation is the description of the current state of the resource and we have we can have multiple representations we can have XML we can have
Json we can have HTML jml and many other representations of our resources so I hope all these principles are very clear now so let us go back and let us summarize we know rest is representational State transfer and it is an architectural style it is not a protocol and it is not a specification which is given by some central body it is very very flexible and lightweight and when we use these design principles to design a web service we get restful web service and it is a set of rules and it guides how a data
or information should be exchanged between two applications so rest was a term which was coined by Roy Fielding in the year 2000 and in his doctoral thesis he introduced the concept of rest so I hope we are now very clear with rest and restful web services and we have decoded all the basic concepts I will meet you in the next session thanks for watching