Django, Day - 03

Django, Day - 03

Rest API

1) What is REST?

REST (Representational State Transfer) is a design approach for creating web services that use URLs to access and manipulate resources through standardized actions like reading, creating, updating, and deleting, with no memory of previous interactions. It promotes simplicity and consistency in web communication.

In simple words...
It is an architectural guideline for developing web API.

Example:

Think of REST-like ordering a pizza:


2) What is REST API?

A REST API is a way for different software applications (or "restaurants") to communicate and exchange data with each other over the internet, using a standardized menu-like structure for making requests and receiving responses.

In simple words...
The API which is developed using REST is known as REST API/ RESTful API.

Example:


3) How does REST Web API work?

  • Client makes HTTP request to API

  • API will communicate to Web Application/Database (If needed)

  • Web Application/Database provides required data to API

  • API returns Response Data to the Client


4) Let's learn a little more about REST APIs


Let's understand this....

In HTTP five methods are commonly used in a REST-based Architecture i.e., POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations respectively. There are other methods which are less frequently used like OPTIONS and HEAD.

GET: Think of it like looking at a web page to get information. When everything is okay, you get the information you wanted (like a recipe) and a thumbs-up (200 OK). If something's not right, you get a "not found" message (404) or "something's wrong" message (400).

POST: It's like adding a new item to your shopping list. After you've added it successfully, you get a high-five (201) and a note telling you where to find the new item.

PUT: It's like updating your favourite recipe. You send the whole recipe (resource) with changes. If it works, you get a thumbs-up (200), or if you make something new, you get a gold star (201).

PATCH: Think of it as changing just one ingredient in a recipe, not the whole thing. You send a note describing the change, and it gets done. But it's not always safe, and it's not the same every time you ask.

DELETE: Imagine throwing away a recipe you don't want anymore. You say "I don't need this," and it disappears. You get a confirmation (200), but there's nothing left to see (no content).


5) Idempotence

An idempotent HTTP method is an HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. (Again, this only applies to the result, not the resource itself.)


6) Request and Response

GET Request: It's like asking for a list of all students.

  • You send a GET request to the API at "/API/students."

POST Request: It's like handing in a new student's name to add to the list.

  • You send a POST request to the API at "/API/students" with the student's name.

PUT/PATCH Request: It's like giving instructions to change a student's name with a specific ID.

  • You send a PUT or PATCH request to the API at "/API/students/1" with the updated student's name.

DELETE Request: It's like asking to remove a specific student with ID=1 from the list.

  • You send a DELETE request to the API at "/API/students/1."

Will learn more about this, in upcoming blogs :)

Thank you so much for reading πŸ™ŒπŸ™Œ

Linkedin

Β