Mastering the Art of REST APIs: A Complete Overview
Tags: api, programming, webdev, javascript
Published: 2023-08-16 13:55:23

<h2 id="heading-what-is-a-rest-api" class="permalink-heading">What is a REST API</h2>
Suppose you have gone to a restaurant and who do you ask to bring you food or pay the bill or you want something in your food to be changed, you don't directly go to the chef or the manager of the restaurant, you call the waiter and he takes your orders and acts according to it. Here waiter is acting as a medium between you and the restaurant.

Now think of the restaurant as a server and think of you as a client who wants to get some data from any server how do you get that data?? We use APIs for these interactions.

Now the official definition according to your beloved Chatgpt -- API stands for Application Programming Interface. It is a set of rules and protocols that allows different software applications to communicate and interact with each other.

So up until now, we have understood what is an API but then what in the world is REST API?

REST APIs are the ones that specifically follow the principle of REST architecture style, it is mainly based on designing web services. Well if you didn't understand this just remember that REST APIs are primarily used with the HTTP protocol and are designed for transferring data between clients and servers. The data exchanged during this interaction is commonly referred to as a resource, so throughout this blog, I will use the term 'resource' instead of 'data'. Resources are generally transferred in JSON format as it is most widely used and easy to interact on the frontend side via js.

A representation is referred to as the format or structure of a resource, A resource can have many representations for ex:- the data on the server side would be in another format like in SQL/NoSQL, and the same data is transferred in JSON format to the client side.

APIs work on the basis of the request-response model

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689432792187/53c564c1-9bc6-4c7a-80a6-230c527eaf9c.png?auto=compress,format&amp;format=webp" alt="" />
<h2 id="heading-various-uses-of-rest" class="permalink-heading">Various Uses of REST</h2>
So we have understood what is a REST API in theoretical terms now let's look at the practical side of it.

In these requests, we don't mention exactly what we have to do inside our URL these two requests have the same URL but one is for getting the data from the server and the other is to create a new document inside the database.

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692019346428/aefafb9c-6295-4a2d-8655-e9ffdb30f7c3.png?auto=compress,format&amp;format=webp" alt="" />

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692019335693/2590ad37-5d2a-40ef-b73c-f94832b597ae.png?auto=compress,format&amp;format=webp" alt="" />

REST APIs are used for interaction via HTTP protocol there are different methods to interact with the server, you might want to get some data or update/delete something. These are some reasons for using REST APIs

There are different methods of HTTP by which you can use REST API:-

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689431946607/b445d417-b2a2-4f78-8bdd-32ed8ddea771.png?auto=compress,format&amp;format=webp" alt="" />

Let's learn them one by one
<h3 id="heading-1-get" class="permalink-heading">1 ) GET</h3>
Get is used for getting some resources from the server, the GET method can be used for getting all the data to some specific data. To retrieve specific data, we utilize endpoints within the URL and include path and query parameters.

We have used several technical terms, so let's take a moment to define them.

1 Endpoints - API endpoints, also known as URL endpoints or simply endpoints, are specific URLs (Uniform Resource Locators) or routes that represent distinct resources or functionalities exposed by an API. Endpoints define the entry points or access points to interact with the API and perform various operations.

2 Path Parameter - Also known as route parameter it is used within the API endpoints that help in dynamic/variable values to be passed as part of URL.

3 Query Parameter - A query parameter is a component of a URL that is used to provide additional information or parameters to an API endpoint. Query parameters are appended to the end of a URL with a question mark "?" and are typically in the form of key-value pairs.

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689432620647/2a2cad6d-cb61-4914-b302-f95c4445329c.png?auto=compress,format&amp;format=webp" alt="" />

We have understood what get API (ps - everything you type in the URL is always a GET request, you can't make any other method request to the server from the URL) is but what does the request look like

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689432876480/a04bc491-6a70-4ef7-a3c6-93552557f986.png?auto=compress,format&amp;format=webp" alt="" />

Here localhost:8000 -- is a domain name and after that all that is path parameters, if you are wondering what tool is this it is "postman"
<h3 id="heading-2-post" class="permalink-heading">2) POST</h3>
Post request is used to send data to a server to create or add any new entity to the database, for ex - if you visit "Youtube" you will have to sign up for this the frontend side of your application has to send a post request to the server of youtube to register your details in their database.

A Post request has a body as you will be sending some data to the server in this case for signup you might be sharing your name, username, email Id, and password.

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692018598984/c39f027c-f512-49b0-a8bc-083ba0196599.png?auto=compress,format&amp;format=webp" alt="" />
<h3 id="heading-3-put-patch" class="permalink-heading">3) PUT / PATCH</h3>
A Put and Patch request is used to update the data inside the database the difference between these two is that in a Patch request you only have to send the attributes you want to update, and in a Put request you have to send the entire object you want to update.

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692019092735/02c24497-dca3-4c32-9e5e-1644bb9b6121.png?auto=compress,format&amp;format=webp" alt="" />

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692019153469/6762ca79-fafc-42d7-bcc4-4f156b3c1f83.png?auto=compress,format&amp;format=webp" alt="" />
<h3 id="heading-4-delete" class="permalink-heading">4) DELETE</h3>
As the name suggests a Delete request is made to delete something from the database, the delete request requires a query parameter with the unique id of the object the user wants to delete.

<img class="image--center mx-auto" src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692019431160/9c926105-1899-42d8-836d-dff139dfc851.png?auto=compress,format&amp;format=webp" alt="" />
<h3 id="heading-thanks-for-reading-this-blog" class="permalink-heading">Thanks for reading this blog.</h3>
If you find this post exciting, find more exciting posts on Learnhub Blog; we write everything tech from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain.

This article was written by <a class="font-medium text-slate-900 dark:text-white" href="https://hashnode.com/@Ankur25">Ankur Sharma.</a>
<div class="mb-5 flex w-full flex-row items-center justify-center md:mb-0 md:w-auto md:justify-start"></div>