DEV Community

Margia Sultana
Margia Sultana

Posted on

CRUD Operation

CRUD stands for create, read, update and delete. These are the four basic functions of persistent storage. Organizations that keep track of customer data, accounts, payment information, and other records require data storage hardware and applications that provide persistent storage. This data is typically organized into a database.

Create:
To create resources in a REST environment, we most commonly use the HTTP POST method. POST creates a new resource of the specified resource type. let’s imagine that we are adding a new package to the stored list of packages for this tourism website, and the package objects are stored in a packages resource. If we wanted to create a new item, we would use a POST request.

Request:
POST http://www.tourism.com/packages/
Body -
{
  "package": {
    "name": “Goa Tour”,
    "price":20,000
  }
}

Enter fullscreen mode Exit fullscreen mode

So, This creates a new package with a name value of “Goa Tour” and a price value of 20,000BDT.
After Created successfully the server should return a header with a link.For example,

Response:
Status Code - 201 (CREATED)
Body -
{
  "package": {
    "id": 123,
    "name": “Goa Tour”,
    "price":20,000
  }
}
Enter fullscreen mode Exit fullscreen mode

So, for this response, we can see that the newly added package was created successfully.

Read:
We use the GET method to read resources in a REST environment.GET can be used to read an entire list of items. If we call GET on the same information 10 times in a row, you should get the same response on the first call that we get on the last call.

Update:
For UPDATE we use the PUT method for the CRUD operation. For example, if the price of the Goa Tour has gone up, We can do this with a PUT request.


PUT http://www.tourism.com/packages/123
Body -
{
  "package": {
    "name": “Goa Tour”,
    "price":21,000
  }
}
Enter fullscreen mode Exit fullscreen mode

This request should change this package with id 123 and now still have the name “Goa Tour”, but the price value should now be 21,000, whereas before it was 20,000. The response includes a Status Code of 200 (OK) to signify that the operation was successful. For example,

Response:
Status Code -200 (OK)
Body -
{
  "package": {
    "name": “Goa Tour”,
    "price":21,000
  }
}

Enter fullscreen mode Exit fullscreen mode

Delete:

For Delete, we use the DELETE method for the CRUD operation. For example, if we want to remove this Goa tour package we know has an id of 123.

Request:
DELETE http://www.tourism.com/packages/123
Enter fullscreen mode Exit fullscreen mode

So after this DELETE call would return the original list of dishes with the {"id": 123, "name": “Goa Tour”, "price": 21,000} entry removed. All other dish objects in the package resource should remain unchanged.

Oldest comments (0)