What is a Rest API? How REST API work?
As a developer, you might have often come across the word REST API. REST is an architectural strategy used for building web services. In this article, let us discuss REST in detail.
What is Rest?
REST stands for REpresentational State Transfer. It is an architectural strategy often used in developing modern web services. REST works on simple HTTP and employs various HTTP methods including GET, POST, PUT, DELETE to perform various actions. Another interesting thing about REST is that it has been built on top of URLs. So any request to a rest based web server needs to be made to a particular URL on that server.
Unlike SOAP(Simple Object Access Protocol), REST is not a fixed protocol and just an architectural style that is often used with variations best suiting the developer.
How REST Works?
REST is rather very simple to understand. There are URLs where you can get data from the server. To give you a more clear perspective, here is an example.
Visit the following URL on your browser https://jsonplaceholder.typicode.com/posts/. You will see something similar to the image below.
When you visit the URL mentioned, you are making an HTTP request to the server. This request is decoded by the server and an appropriate response(JSON data in this case) is sent back to you. You need not do anything other than visiting a URL. The same response could also be achieved by using curl from a terminal as shown below.
So I am sure this makes it very clear that a REST API will return an appropriate response on a valid request. It does not matter which platform or state you make the request from, if the request is valid, you will get a response. Hence, REST often said to be stateless in nature. As it does not depend on the state of the client.
The query parameters in URL are often used to specify what data do we precisely want. For example, the URL https://jsonplaceholder.typicode.com/posts returns all the posts in the database. But if we visit the URL https://jsonplaceholder.typicode.com/posts/6 we get the following response/data.
We just received the data for the post with id number 6. However, this rule does not apply for every REST API in the world. The way of querying data from an API differs from API to API. Most public APIs have a documentation on how to use them.
Uses of REST API
REST APIs have become the backbone of modern web services. Almost all web services already have or are being converted to REST APIs. REST helps in segregating between the front and the back-end. Modern web services span across mobile apps, websites, desktop applications etc. REST API’s serve as the common back-end solution to all of these.
A REST API may also be made public for use by others. For example companies like Facebook, Twitter and Google have their public API’s made for others who wish to fetch data from their servers.
REST is an amazing architectural strategy for building server backends that can be used to fetch data by anyone who makes a valid request. REST is built completely on top of HTTP and works seamlessly with most tools and software out there. I hope you now have a clear idea of REST and REST API. If you still have a doubt regarding REST, drop in a comment and I’ll be there for you.