Saturday, November 16, 2024
Google search engine
HomeGuest BlogsSend a curl DELETE Request {With Example}

Send a curl DELETE Request {With Example}

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.

Send a DELETE Request Using curlSend a DELETE Request Using curl

Prerequisites

  • Access to the command line/terminal with administrator user privileges.
  • NodeJS and NPM installed and updated.
  • Access to a text editor.

Note: To install NodeJS and NPM, follow our OS-based guides for:

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
json-server --watch database.json terminal outputjson-server --watch database.json terminal output

The server starts locally, listing the following two pages:

  • Resources at http://localhost:3000/people contains the data defined in the database.json file.
json database on localhost browser pagejson database on localhost browser page
  • Home at http://localhost:3000 contains the landing page with the message that the server is up.
json server runningjson server running

7. In a new terminal tab, send a DELETE request using curl:

curl -X "DELETE" 'http://localhost:3000/people/3'
curl delete request terminalcurl delete request terminal

The terminal outputs an empty set. Check http://localhost:3000/people to confirm the third entry is no longer there.

json database deleted entryjson database deleted entry

The server session in the command line/terminal shows the DELETE request with a server response of 200 (success).

json server curl delete request response 200json server curl delete request response 200

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.

Was this article helpful?
YesNo

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments