Why we need REST API?
For example:- We all are using different apps on regular basis, in which the data present is not static, it changes or gets updated according to our request. From where, we get this data, answer is from a server or web server.So, the client requests the server for a required information via an API and then server sends back response to the client. Over here the response is in HTML page format.Now imagine you request server to know weather of a specific country, specific state with specific timing, now if the server returns you the HTML page, you won’t like it, so client prefer the data to be returned in form of a structured format rather than a complete HTML page, So now the data returned should be in a JSON and XML format.Why JSON and XML?
Because they have a proper structure in which the data represented .
Here the structure of XML and JSON response sent by the server , in which XML format follows a hierarchical structure, whereas JSON has basically format of an object where object values are returned to the client.
Here the only problem is , you required a lot of method to get the data, to put the data, methods like doPost, doGet etc. So, it seems easy for a single request, but when you request a lot of data together it becomes, difficult.To solve this REST API comes into picture.
REST API : —
creates an object and thereafter sends the values of the object in the response to the client request.It is an architectural style as well as an approach for communication purpose that is often used in various web services development.
REST- Representational State Transfer from where that term come from, when a client request for any information from the server via API, the server finds out whether the data is present or not, once the server finds out the data, Now the API creates an object of this particular data, now what is happening, we are creating an object and then object have some values , and these values are sent to the client, so that is the state of an object which is sent to the client, so each and every time you don’t have to generate new object, so you are just passing the state of the object, that’s where the term comes, representational state transfer.
Features of REST API:-
Simpler than SOAP
Proper Error Messages.
Principles of REST API: —
- Uniform interface-
This constraint has 4 parts:
- The request sent to the server has to include a resource identifier.
- The response the server returns include enough information so the client can modify the resource
- Each request to the API contains all the information the server needs to perform the request, and each response the server returns contain all the information the client needs in order to understand the response.
- Hypermedia as the engine of application state —means the server can inform the client , in a response, of the ways to change the state of the web application. If the client asked for a specific user, the server can provide not only the state of that user but also information about how to change the state of the user, for example how to update the user’s name or how to delete the user.
2. Client — server separation — means the interaction between client and server only takes place, in the form of requests and response, when client sends the request the server should wait for the request, it is not like server starts sending response or state of some resources on its own, there should a pattern.
3. Stateless — Stateless means the server doesn’t remember about the user.It doesn’t remember if the user of the API already sent a GET request for the same resource in the past, which resources the user of the API requested before.
4. Layered system — It allows an application to be more stable by limiting the component behavior this architecture enables load balancing . It also helps in enhancing the applications security as components in each layer cannot interact beyond the next immediate layer they in.
5. Cacheable — In order to provide a better performance the applications are often made as cacheable it is done by labeling the response server as cacheable or non cacheable either implicitly or explicitly , if the response is defined as cacheable then the client can reuse the response data for equivalent responses in the future it also helps in preventing the reuse of steal data.
Thanks for giving your valuable time.