Introduction
cURL (client URL) is a command-line utility for transferring data to and from a server. The tool allows communication with a web or application server and sending method requests directly from the terminal.
The HTTP DELETE method request sends a signal to the originating server to delete a resource.
This tutorial explains how to send a curl DELETE request through an example REST API JSON server.
Prerequisites
- Access to the command line/terminal with administrator user privileges.
- NodeJS and NPM installed and updated.
- Access to a text editor.
Curl DELETE Request Syntax
The basic syntax to send a DELETE request method using curl
is:
curl --request "DELETE" <URL>
Alternatively, use the shorthand version:
curl -X "DELETE" <URL>
The curl
command sends a DELETE request to the HTTP server, deleting the page or entry at the provided URL.
Curl DELETE Request Example
The example below demonstrates how the curl DELETE request works. The example creates a fake REST API server using the JSON server package.
Note: Need a real and inexpensive server to set up a testing environment? Check out Bare Metal Cloud instances that start as low as $0.10/h.
Then, follow our instructions on how to set up a sandbox envrionment on BMC. Alternatively, see how to create a load balancer and test the connection using curl.
1. Open the terminal (CTRL+ALT+T).
2. Run the following command to install the json-server
library using the NPM package manager:
sudo npm install -g json-server
3. Open a text editor and create a database.json file. If you're using nano, run:
nano database.json
4. Add the following data:
{
"people": [
{
"id": 1,
"name": "Matthew"
},
{
"id": 2,
"name": "Mark"
},
{
"id": 3,
"name": "Luke"
}
]
}
The file represents a mock database of people with unique IDs and names.
5. Save the file and close the text editor.
6. Run the following command to start the server:
json-server --watch database.json
The server starts locally, listing the following two pages:
- Resources at
http://localhost:3000/people
contains the data defined in the database.json file.
- Home at
http://localhost:3000
contains the landing page with the message that the server is up.
7. In a new terminal tab, send a DELETE request using curl
:
curl -X "DELETE" 'http://localhost:3000/people/3'
The terminal outputs an empty set. Check http://localhost:3000/people
to confirm the third entry is no longer there.
The server session in the command line/terminal shows the DELETE request with a server response of 200 (success).
Attempting to delete non-existing data results in a server response 404 (not found).
Conclusion
After following the steps from this tutorial, you understand how to send a DELETE request through the command line using the curl
command.
Next, see how you can change the user agent with curl.