What is Rest Api - A beginner's guide
Tags: beginners, rest, api
Published: 2022-11-18 06:59:45

For many beginners in tech, REST is a foreign concept, sometimes, it's another buzzword in the layers of complex concepts to learn. As a non-computer science graduate myself, i had the same feeling when i was told to consume a REST api my team created in a hackathon.

This is why in this article, I aim to explain REST in the simplest language possible.

- [What REST Api is](#what_is_rest)
- [Why REST Api](#why_rest)
- [Principles of REST Api](#rest_principle)
- [REST Api methods](#rest_method)


## What is a REST Api?<a name="what_is_rest"></a>
Let start by identifying what a REST Api is not.
* REST Api is not a protocol.
* REST Api is not a standard.

Now, that's no longer in the way. *What is a REST Api?*
A REST Api is an application programming interface that conforms to the constraints of the REST architectural style and allows interaction with RESTful web services(web apps based on the REST architecture). As you may already know, REST is the acronym for **Representational State Transfer**. The REST architectural constraints can be implemented in different ways.

## Why REST Api?<a name="why_rest"></a>
As the saying goes, 

> Necessity is the mother of invention - Plato

It's important that you understand the purpose of the creation of the REST architectural style.
Before REST was created, SOAP(Simple Object Access Protocol) was the protocol of choice to facilitate data transfer between application built with different languages and on different platforms.

> In networking, a protocol is a set of rules for formatting and processing data

Being a protocol, SOAP imposes some built-in rules that increase it complexity and overhead which sometimes lead to longer load time.

As an advantage, SOAP apis are considered to be more secure than REST api as it uses WS-security for transmission along with Secure Socket Layer(SSL) while REST uses SSL and HTTPS (this advantage is still debated in some tech ecosystem) and it was designed to power large enterprise applications.

As the internet explosion continues and more smart devices are created, so is the need to make data transmission more seamless. Then came the REST architectural style. It was designed to suit the needs of the modern day technologies like IOT, mobile device etc.

Using Rest apis, data transmission can be in several formats (JSON, HTML, XML,or plain text) over HTTP. All communication done via REST api uses HTTP request and requires less bandwidth compare to SOAP.

## Principles of REST Api <a name="rest_principle"></a>
An application is considered restful if it implements the 6 REST architectural principles which were proposed by computer scientist Roy Fielding.

1. A RESTful application should have a client-server communication in order to access resources. Usually, the client is a front-end (user-facing) application e.g. a mobile app or a single page application (SPA). The Server is the back-end application which houses the resource. It provides a URL, which is the path  to resource.

2. A RESTful application should be stateless. Requests sent from a client to the server should contain all the required information to make the server understand the request, no client information is stored between requests and each request is separate and unconnected. HTTP request contains pieces of information such as authorization, caching, cookies etc.

3. A RESTful application should provide a uniform interface between components such that information is transferred in a standard form. This requires:
  - Resource identification
  - Resource Manipulation using representations i.e the client can manipulate the resource via the representation they receive because the representation contains enough information to do so.
  - Self-descriptive messages.
  - Hypermedia as the engine of application state i.e the client should be able to use hyperlinks to find all other currently available actions they can take after accessing a resource.

4. A RESTful application should provide cacheable data that streamlines client-server interactions.

5. A RESTful application should provide a layered system that organizes each type of server (those responsible for security, load balancing, etc.) involved in the retrieval of requested information into hierarchies invisible to the client.

6. Code-on-demand. This is optional. It is the ability to send executable code from the server to the client when requested, extending client functionality.

## REST Api Methods<a name="rest_method"></a> 
In HTTP, there are five methods that are commonly used in a REST-based architecture. These methods, as shown in the image below, correspond to CRUD operations.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgnts432x6az1bk5hvhl.jpg)

- **POST**: The POST method is often utilized to **create** new resources.
- **GET**: The GET method is used to **read** or retrieve a representation of a resource.
- **PUT**: This method is used to **update** a resource. PUT method can also be used to perform a create action if the specified resource is non-existent.
- **PATCH**: This method is used to modify(**update**) resource. This is different from the action performed using the PUT method. The PUT method replaces the specified resources entirely with the new information provided in the request body if the resource exist or creates the resource if not. However, PATCH methods modifies specific components of the resource based on the information provided in the  HTTP request body.
- **DELETE**: It is used to **delete** a resource identified by a URI.

I hope you are able to get the information you needed. Watch out for my next article. I will be happy to receive your feedback and comments. Thanks for reading.