REST stands for REpresentational State Transfer. It is a popular architectural approach to create your API’s in today’s world.
The acronym REST stands for REpresentational State Transfer. It was term originally coined by Roy Fielding, who was also the inventor of the HTTP protocol. The striking feature of REST services is that they want to make the best use of HTTP. Let’s now have a quick overview of HTTP.
Let’s open up the browser and visit a web page first:
And then click on one of the result pages:
Next, we can click on the link on the page we end up in:
And land upon another page:
This is how we typically browse the web.
When we browse the internet, there are a lot of things that happen behind the scenes. The following is a simplified view of what happens between the browser, and the servers running on the visited websites:
When you enter a URL such as https://www.google.com
in the browser, a request is sent to the server on the website identified by the URL. That server then responds with a response. The important thing is the formats of these requests and responses. These formats are defined by a protocol called HTTP — Hyper Text Transfer Protocol.
When you type in a URL at the browser, it sends out a GET request to the identified server. The server then replies with an HTTP response that contains data in HTML — Hyper Text Markup Language. The browser then takes this HTML and displays it on your screen.
Let’s say you are filling in a form present on a web page with a list of details. In such a scenario when you click the Submit button, an HTTP POST request gets sent out to the server.
HTTP provides the base layer for building web services. Therefore, it is important to understand HTTP. Here are a few key abstractions.
A resource is a key abstraction that HTTP centers round. A resource is anything you want to expose to the outside world through your application. For instance, if we write a todo management application, instances of resources are:
When you develop RESTful services, you need to focus your thinking on the resources in the application. The way we identify a resource to expose, is to assign a URI — Uniform Resource Identifier — to it. For example:
/user/ranga
/user/Ranga/todos
/user/Ranga/todos/1
REST does not worry about how you represent your resource. It could be XML, HTML, JSON, or something entirely different! The only important thing is you clearly define your resource and perform whatever actions that are supported on it by making use of features already provided by HTTP. Examples are:
POST /users
DELETE /users/1
GET /users
GET /users/1
A significant point to note is that with REST, you need to think about your application in terms of resources:
Here is how a REST service is generally implemented:
REST focuses on resources and how effectively you perform operations on them using HTTP.
HTTP defines the following for a request:
For the response, HTTP defines the:
The method used in a HTTP request indicates what action you want to perform with that request. Important examples are:
A status code is always present in a HTTP response. Common examples are:
In this article, we had a high-level look at REST. We stressed the fact that HTTP is the building block of REST services. HTTP is a protocol that is used to define the structure of browser requests and responses. We saw that HTTP deals mainly with resources that are exposed on web servers. Resources are identified using URIs, and operations on these resources are performed using verbs defined by HTTP.
Finally, we looked at how REST services make the best use of features offered by HTTP to expose resources to the outside world. REST does not put any restrictions on the resource representation formats or on the service definition.
#rest #api #web-development #web-services