Interacting with a backend server is an important process in web development and REST APIs are one of the most widely used methods of talking to a backend server. REST means Representational State Transfer and API means Application Programming Interface. Fancy names, right? So lets dive in for more details.
APIs as stated above means Application Programming Interface. These are basically programs which are written and exposed for other programs to communicate with them. Other programs can connect with these "interfaces" and data exchange can take place. It is not necessary that only frontend and backend can communicate with each other. Different backends can also communicate with each other. In fact, there can be communication between different subsystems or services of one backend also. One good example is microservices. To facilitate this communication, REST APIs are one of the best mediums.
REST is just a set of conventions for designing, exposing and consuming an API. It has some principles and methods which makes the communication a breeze.
Here's the simple process - API is made and an endpoint is exposed for clients to use this API, client sends a request to this endpoint, API starts its process and the requested data is sent by the REST API in a response. Broadly, this is the request-response cycle. Each time client sends a new request, server treats it as a brand new request and it has no idea of the client state. No client data is stored on the server during any request. Client tells server what it needs and server supplies it with that. No personal relationships! This refers to statelessness, the soul of REST APIs!
Requests and responses work on HTTP, the Hypertext Transfer Protocol. And there are multiple HTTP methods like GET, POST, PUT, PATCH, DELETE and OPTION. These methods are used to perform the CRUD (Create, Read, Update and Delete) operations. Client uses these methods to make request to the API endpoints and each method instructs the REST API about the kind of operation that it needs to perform.
Each layer has some functions defined in them and when a request hits an API endpoint, each layer helps navigate it to an appropriate method in the next layer and finally in the DAO layer, it can have access to the database. However, it is not all necessary to make such a layered architecture in small applications. But as our application grows, such an architecture really helps, especially in maintaining the server.
Even after reviewing this post a couple of times, I feel like its not upto the mark 😅 Maybe because I am not adding any code examples in it. I'll update it in future. So, yeah, that's all for now. Hope you got some insights through this post. I genuinely feel like I have to work more on my writing style. But thanks for reading anyways!