How to Send a DELETE Request With curl
Here are practical examples and demonstrations of sending DELETE requests with Curl.
β Sagar Sharma
The cURL is often used to send and receive data from web servers and almost every Linux user with fair knowledge can benefit from its offering.
But did you know that you can even send delete requests to the domains?
But just showing off commands won't make any sense, so I'm going to demonstrate the whole process with the local database!
Send DELETE Request using cURL
You must be familiar with the general syntax of curl if you followed our other guide of ours explaining how to use curl with examples.
And to send a DELETE request, you just have to pair the --request
with Β "DELETE"
as shown:
curl --request "DELETE" <URL>
Similarly, you can also use a shorthand version replacing --request
with -X
:
curl -X "DELETE" <URL>
It is time to put those commands to work. And as I mentioned earlier, I'll be setting up a local database for this guide.
Setting up local JSON server
For the installation of the JSON server, I'm going to utilize the npm package manager which can easily be installed in ubuntu using the given command:
sudo apt install npm
Now, let's use the npm package manager to install JSON:
sudo npm install -g json-server
Now, I'm adding some basic employee data to make it easier to grasp:
nano database.json
{
"Employee": [
{
"id": 1,
"name": "Chris"
},
{
"id": 2,
"name": "Mark"
},
{
"id": 3,
"name": "Titus"
}
]
}
Save the file and start the server by using the given command:
json-server --watch database.json
You can use the link given by Resources in your browser to check for available data.
Sending DELETE request
To send a DELETE request using curl, you'll need to open another terminal window.
For example, if I want to delete data related to a second employee, my command would be as follows:
curl -X "DELETE" 'http://localhost:3000/Employee/2'
You can also visit the same Resources HTTP link in the browser and find the details related to 2nd employee no longer available:
As you can see, it no longer shows a 2nd employee named Mark.
Wrapping Up
At this point, you must be familiar with what curl does. But have you ever wondered about the difference between wget and curl? Well, I already have the answer:
While I tried my best to keep the explanation guide on sending DELETE requests using curl, if you still have any queries, you are most welcome to with your queries in the comments.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.