What is a REST API?

Mathursan Balathas
3 min readApr 3, 2021

REST(REpresentational State Transfer) is a web service-based architecture used for data exchange between applications or systems over the Internet. The web services based on REST Architecture are known as RESTful web services. These web services are built around resources(a resource can be Text Files, Html Pages, or Images.) and those resources are identified by a URI and accessed by a common interface that uses the HTTP protocol. Simply saying, this architecture is to provide a client-server interaction via an API that has some set of protocols for the communication between the resources. As that API is built based on REST architectural constraints and allows REST services it's called REST API.

When a request is made through REST API, it delivers a ‘representation of the state of the resource’ to the endpoint.REST uses several formats of representation to represent a resource such as JSON (Javascript Object Notation), HTML, XML, or text. Here JSON is the most common and popular one. This API can be implemented in a variety of ways by the developers according to their business logic.

For an API to be considered as REST API, it should be created by applying the REST architectural constraints as well. The following are six constraints for a REST API.

Uniform interface The information should be transferred in a common way through the interface, to achieve this there are four aspects that should be considered,

  1. Identification of resources through URI.
  2. Manipulation of resources through representations.
  3. Self-descriptive messages for each request-The request should tell the server what to do.
  4. Hypermedia As The Engine Of application State(HATEOS)-response should include links of other related resources for the client.

Stateless

The server doesn’t save any session data. Session state will be kept on the client-side. This is where self-descriptive messages come in handy to help the server.

Cacheable

The server determines whether a response can be cachable.if so the client is allowed to cache it on its side.

Client-Server

This insists that the API should have the client-server model. Also they should be independent.

Layered System

A layered system makes each layer only to be aware of the next immediate layer. This limits and hides the complexity of service from the client which makes the system highly scalable.

An overview of a REST architecture

HTTP Methods

REST services use HTTP verbs to perform operations on the resources. Each of those verbs invokes certain methods on the specified resource. The following are the common HTTP verbs that are used in REST services.

HTTP requests made using a http file

GET

This is to fetch a resource with read-only access. This method can be used in two ways, one is to fetch the whole list of resources and the other one is to fetch a particular resource by identifying its unique identifier(like a key-value) which will be attached in the request URI.

The following URI will give you an idea about fetching resources using a request. The prefix set for this URI is ‘posts’(the location where the resource resides).

Eg: GET http://localhost:3000/posts-This request will return all the resources.

Eg2: GET http://localhost:3000/posts/4747425e-3694-46bd-9583-e77ae4e5581b -This will return the resource of the specified id ‘4747425e-3694–46bd-9583-e77ae4e5581b’

POST

This method is to create(insert) a new resource. The client has to attach the details to be inserted along with the request.

Eg: POST http://localhost:3000/posts

PUT

This method will replace an existing resource by updating it by referring to the identifier of the resource in the request’s URI.

Eg: PUT http://localhost:3000/posts/4747425e-3694-46bd-9583-e77ae4e5581b

DELETE

This method will remove the resource with the specified identifier in the request URI.

Eg: DELETE http://localhost:3000/posts/4747425e-3694-46bd-9583-e77ae4e5581b

OPTIONS

This will return all the allowed operations.

HEAD

This will return the response header for the given request. In HTTP requests, headers also play a crucial role as they consist of few important details like URI, caching, cookies, and more. So here when a HEAD method is called from the request it will return the response header.

--

--

Mathursan Balathas

Associate Software Engineer at Sysco LABS Sri Lanka | Undergraduate at SLIIT