Mastering the Art of REST APIs: A Complete Overview

Scofield Idehen - Aug 16 '23 - - Dev Community

What is a REST API

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

Various Uses of REST

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.

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:-

Let's learn them one by one

1 ) GET

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.

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

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"

2) POST

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.

3) PUT / PATCH

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.

4) DELETE

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.

Thanks for reading this blog.

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 Ankur Sharma.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .