Table of contents

How do I use Curl to send a DELETE request?

How to Send DELETE Requests with Curl

The curl command supports DELETE requests using the -X DELETE option. DELETE requests are commonly used in REST APIs to remove resources from the server.

Basic DELETE Request

curl -X DELETE https://api.example.com/users/123

This sends a DELETE request to remove user with ID 123.

DELETE with Authentication

Most APIs require authentication for DELETE operations:

Bearer Token Authentication

curl -X DELETE \
  -H "Authorization: Bearer your_access_token" \
  https://api.example.com/users/123

API Key Authentication

curl -X DELETE \
  -H "X-API-Key: your_api_key" \
  https://api.example.com/users/123

Basic Authentication

curl -X DELETE \
  -u username:password \
  https://api.example.com/users/123

DELETE with Request Body

Some APIs accept data in DELETE requests (though this is less common):

JSON Data

curl -X DELETE \
  -H "Content-Type: application/json" \
  -d '{"reason": "user_requested", "notify": true}' \
  https://api.example.com/users/123

Form Data

curl -X DELETE \
  -d "reason=inactive&force=true" \
  https://api.example.com/users/123

Complete Example with Response Handling

# Delete with full response details
curl -X DELETE \
  -H "Authorization: Bearer your_token" \
  -H "Content-Type: application/json" \
  -w "HTTP Status: %{http_code}\nResponse Time: %{time_total}s\n" \
  -v \
  https://api.example.com/users/123

Common Response Codes

  • 200 OK: Resource deleted successfully with response body
  • 204 No Content: Resource deleted successfully (no response body)
  • 404 Not Found: Resource doesn't exist
  • 403 Forbidden: Insufficient permissions
  • 405 Method Not Allowed: DELETE not supported for this endpoint

Error Handling

# Save response and check exit code
curl -X DELETE \
  -H "Authorization: Bearer token" \
  -o response.json \
  -w "%{http_code}" \
  https://api.example.com/users/123

# Check if request was successful
if [ $? -eq 0 ]; then
    echo "DELETE request completed"
else
    echo "DELETE request failed"
fi

Best Practices

  1. Always use HTTPS for DELETE requests containing sensitive data
  2. Include proper authentication headers
  3. Check API documentation - not all endpoints support request bodies with DELETE
  4. Handle different response codes appropriately
  5. Use verbose mode (-v) for debugging

Alternative: Using --request

You can also use --request DELETE instead of -X DELETE:

curl --request DELETE \
  --header "Authorization: Bearer token" \
  https://api.example.com/users/123

Both approaches are equivalent and work identically.

Try WebScraping.AI for Your Web Scraping Needs

Looking for a powerful web scraping solution? WebScraping.AI provides an LLM-powered API that combines Chromium JavaScript rendering with rotating proxies for reliable data extraction.

Key Features:

  • AI-powered extraction: Ask questions about web pages or extract structured data fields
  • JavaScript rendering: Full Chromium browser support for dynamic content
  • Rotating proxies: Datacenter and residential proxies from multiple countries
  • Easy integration: Simple REST API with SDKs for Python, Ruby, PHP, and more
  • Reliable & scalable: Built for developers who need consistent results

Getting Started:

Get page content with AI analysis:

curl "https://api.webscraping.ai/ai/question?url=https://example.com&question=What is the main topic?&api_key=YOUR_API_KEY"

Extract structured data:

curl "https://api.webscraping.ai/ai/fields?url=https://example.com&fields[title]=Page title&fields[price]=Product price&api_key=YOUR_API_KEY"

Try in request builder

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon