Web Dev Basics: REST API & CRUD operations
Tags: webdev, api, learning, rest
Published: 2023-06-21 12:52:10

Video version (I'm not a master yet, but I'm just learning, so don't judge strictly =):
{% embed  https://www.youtube.com/watch?v=8M4Dc7JR1HI&t=3s %}

## What the... REST? 😴

Acronym REST API stands for Representational State Transfer Application Programming Interface - it is an architectural style for designing networked applications that communicate over HTTP. 


![REST API - Representational State Transfer Application Programming Interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/887qnyi4y011unthr0v6.png)


The API is a set of rules that defines how the client and server interact. 
The client sends a request that includes a HTTP method, which tells the server what kind of operation to perform. The server sends a response back to the client, usually in the form of JSON or XML data.

It allows clients to access and manipulate resources on a server using a standardized set of operations.

![Icons of Edge, Chrome, Curl and Postman apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fknwxx8rvg7gu7456q6o.png)


When you browse the web, you're using a client (the web browser) to send HTTP requests to a server (the website's server), and then receive HTTP responses. 
This process of sending requests and receiving responses mirrors the way RESTful APIs work.

## CRUD

REST uses, or better to say implements CRUD operations.

(Create, Read, Update, Delete) refers to the four basic operations performed on data: creating new resources, reading existing resources, updating existing resources, and deleting resources. 


![(Create, Read, Update, Delete) refers to the four basic operations performed on data: creating new resources, reading existing resources, updating existing resources, and deleting resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzdgjahuj7kgu8sskwpg.png)

So, you can create a new account or user on some website, update your data, read this data, and delete your data if the site allows to do it.

There is a list of HTTP methods and how they map to CRUD operations


- **GET**: The GET method is used to retrieve data from a server. It's a read-only operation and doesn't affect the data on the server. In the context of CRUD operations, GET corresponds to Read.
- **POST**: The POST method is used to send data to a server to create a new resource. The server generates a unique id for the new resource and returns this id to the client. In CRUD operations, POST corresponds to Create.
- **PUT** and **PATCH**: PUT and PATCH methods are used to update a resource on the server. The client sends an entire updated resource to the server. The server replaces the existing resource with the one sent by the client. In CRUD operations, PUT and PATCH corresponds to Update.

In both cases, you're trying to update a resource , but with PUT, you replace everything, whole resource. With PATCH, you only change the parts you need to.

Example with broken toy 😥: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6esix1sc57nx7btg1my6.png)

**PUT**: Think of it like replacing an old toy with a new one. You take out the old toy completely and put a new one in its place.
**PATCH**: It's like fixing a toy. Instead of replacing the whole thing, you just fix the broken parts.

- **DELETE**: The DELETE method is used to delete a resource from the server. The server deletes the specified resource and usually sends back a status to let the client know whether the deletion was successful. In CRUD operations, DELETE corresponds to Delete.

## Conclusion

These methods, and REST APIs in general, provide a standardized way for clients and servers to communicate with each other, making it easier to develop and integrate networked applications.

In essence, REST APIs are a fundamental tool that enable smooth and efficient communication between different parts of a web application or different applications entirely. 
They employ HTTP methods, neatly corresponding to CRUD operations that are central for data manipulation.


**Thanks for reading, hope to see you again 😼**