Let’s say the mobile team or the front-end team comes to us and asks for some REST API to get the dynamic data from the backend.
What do we do?
I mean, of course, we would ask for the requirements, right? Like what data they would want and what would be the methods, how we would return the response, and so on.
And then the million-dollar question is to decide the endpoints right?
Well, that would have been the million-dollar question if we don’t know about REST API or REST Framework.
So in general what we are looking for are CRUD APIs. But what is CRUD?
What is CRUD?
CRUD Stands for
- C => Create
- R => Read
- U => Update
- D => Delete
We usually need CRUD APIs in order to do most of the common operations in our database on the server.
Let’s see an example and understand REST API.
A note-taking app
So, what all APIs we might need?
Endpoint to do the below operations, notably to:
- List the notes
- Get any specific note
- Create a note
- Update a note
- Delete a note
We are going with a very simple app and won’t be going very deep into the minute details.
If we have to make the endpoints how will we make them?
I won’t go into the definitions, you can find them already on the internet, won’t you? I feel an example with an explanation is way more helpful than the other.
But before that let’s revise the basics.
GET method, as the name suggests should be used to get something.
POST method is used to submit an entity for a specific resource. Often when this request is made there are some side effects, such as inserting some data into the database.
PUT method we update everything related to a specific resource with the data send in the request body.
PATCH method we modify a resource partially. So for example, if we are updating a note as in our case with a PUT request we would send the whole note data in the request body.
But what if we only wanted to update the title of the note?
I mean yes, we can send the whole body with the only difference that the title would be the new title in the request body.
But it would be expensive right? The request body would be bigger. What if we can only send the data that we want to update and the rest that we don’t send can stay that way?
Here comes the
PATCH request, where we only send the fields in the request body that we want to update.
When we want to delete a resource we use
We still have a bunch of different HTTP request types or HTTP Verbs you may read about them in the MDN Doc