Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Send a curl DELETE Request {With Example}
The curl DELETE request is an HTTP method that allows the removal of a specified resource from a server. It is commonly used to delete data or content from web services or APIs. The DELETE request requires the URL of the resource to be removed, and additional headers or parameters can be included if needed. When the server receives the DELETE request, it processes the request and removes the desired resource if it exists. A successful DELETE request typically returns an HTTP status code indicating the successful removal of the resource.
DELETE Request Method
The DELETE request method is an HTTP procedure used to remove a specified server resource. It is a fundamental component of the RESTful architecture and is frequently used in web services and APIs. The URL of the resource to be removed is included in the DELETE request. Once the server receives it, the request is processed, and if the requested resource exists, it is deleted. The server then responds with an HTTP status code indicating whether the DELETE operation was successful or unsuccessful.
Basic Syntax
curl --request DELETE <URL>
Alternative syntax using the shorthand -X flag:
curl -X DELETE <URL>
Step-by-Step Implementation
To perform a curl DELETE request with a JSON server, follow these steps:
1. Install JSON Server
Install the JSON server package using npm:
npm install -g json-server
2. Create Database File
Create a JSON file named db.json with sample data:
{
"people": [
{
"id": 1,
"name": "Rati"
},
{
"id": 2,
"name": "Kushagra"
},
{
"id": 3,
"name": "Nandini"
}
]
}
3. Start JSON Server
Navigate to the directory containing db.json and run:
json-server --watch db.json
\(^_^)/ hi! Loading db.json Done Resources http://localhost:3000/people Home http://localhost:3000 Type s + enter at any time to create a snapshot of the database Watching...
4. Execute DELETE Request
Open a new terminal window and execute the curl DELETE command. To delete a specific user with ID 3:
curl -X DELETE http://localhost:3000/people/3
Examples
Basic DELETE Request
curl -X DELETE http://localhost:3000/people/1
DELETE with Headers
curl -X DELETE \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-token" \ http://localhost:3000/people/2
DELETE with Verbose Output
curl -X DELETE -v http://localhost:3000/people/3
Server Response Examples
Successful Deletion (HTTP 200):
DELETE /people/3 200 8.212 ms - 2 GET /people 200 3.449 ms - 87
Resource Not Found (HTTP 404): When attempting to delete a non-existing resource:
DELETE /people/99 404 2.156 ms - 2
Common HTTP Status Codes
| Status Code | Description | Meaning |
|---|---|---|
| 200 OK | Success | Resource successfully deleted |
| 204 No Content | Success | Resource deleted, no response body |
| 404 Not Found | Error | Resource does not exist |
| 401 Unauthorized | Error | Authentication required |
| 403 Forbidden | Error | Insufficient permissions |
Best Practices
Verify resource existence before deletion to avoid unnecessary 404 errors
Include proper authentication headers when working with secured APIs
Use verbose mode (
-v) for debugging and troubleshootingHandle response codes appropriately in your applications
Implement confirmation mechanisms for critical resource deletions
Conclusion
The curl DELETE request is essential for removing resources from servers in REST APIs. It provides a straightforward method to delete data using HTTP protocols. Understanding proper syntax, status codes, and best practices ensures effective resource management and maintains data integrity in web applications.
