How to send delete request using CURL? Code Example

Total
0
Shares

Use curl –request DELETE url command to send delete request using CURL.

CURL is a command line utility which is used to transfer data from client to server. It is used by many major programming languages like php for server to server communication. In this article we will focus on delete curl functionality.

How to delete using curl?

To send a delete request, you can use this command in your terminal –

curl --request DELETE https://www.my_website.com/product/?id=5

In the above command we are using --request flag which helps in specifying that we want to send a DELETE request.

The url for DELETE request are generally of a single entity. Although it’s not a rule. It all depends on how you built your API in the backend. If your API can handle to delete multiple items in one request then you can pass a url which specifies a group of items.

Another form of delete curl request is –

curl -X DELETE https://www.my_website.com/product/?id=5

Here we replaced --request flag with -X. You can follow this manual, if you want to learn more about Curl flags.

Code Example

In this code example we will use the API from this url – https://retoolapi.dev/wSA4yY/data?_page=1&_limit=8.

If you open it in the browser, you might get the json output like this –

[
  {
    "id": 1,
    "Column 1": "Aigneis Gimson"
  },
  {
    "id": 2,
    "Column 1": "Binni Oliver"
  },
  {
    "id": 3,
    "Column 1": "Adelind Meadowcroft"
  },
  {
    "id": 4,
    "Column 1": "Michael Brabham"
  },
  {
    "id": 5,
    "Column 1": "Shelton Raffels"
  },
  {
    "id": 6,
    "Column 1": "Desdemona Masarrat"
  },
  {
    "id": 7,
    "Column 1": "Conchita Fissenden"
  },
  {
    "id": 8,
    "Column 1": "Konstanze Galle"
  }
]

So the endpoint details of the API is –

Request Type Endpoint
URL https://retoolapi.dev
GET /wSA4yY/data
GET filter /wSA4yY/data?Column 1=value
GET by id /wSA4yY/data/1
GET paginate /wSA4yY/data?_page=2&_limit=10
POST /wSA4yY/data
PUT /wSA4yY/data/1
PATCH /wSA4yY/data/1
DELETE /wSA4yY/data/1

To run delete curl on this endpoint, we can use this command –

curl --request DELETE https://retoolapi.dev/wSA4yY/data/1

You will get the output like this –

{}

But for you this will fail because I already deleted this entity. You can try with different id that is available in api.

curl delete

Now if we fetch the records again using GET, then id 1 will not be there –

curl -X GET https://retoolapi.dev/wSA4yY/data?_page=1&_limit=8
[
  {
    "id": 2,
    "Column 1": "Binni Oliver"
  },
  {
    "id": 3,
    "Column 1": "Adelind Meadowcroft"
  },
  {
    "id": 4,
    "Column 1": "Michael Brabham"
  },
  {
    "id": 5,
    "Column 1": "Shelton Raffels"
  },
  {
    "id": 6,
    "Column 1": "Desdemona Masarrat"
  },
  {
    "id": 7,
    "Column 1": "Conchita Fissenden"
  },
  {
    "id": 8,
    "Column 1": "Konstanze Galle"
  },
  {
    "id": 9,
    "Column 1": "Hilario Rawlingson"
  }
]
curl get request with json response

Conclusion

In this article we learned about delete curl request. We saw a live code example of JSON api and performed GET and DELETE operations using curl.