curl PUT, PATCH, and DELETE Requests

Use curl for PUT, PATCH, and DELETE HTTP methods. Update and delete resources via REST APIs with idempotency, ETags, and conditional request patterns.

General

Detailed Explanation

PUT, PATCH, and DELETE with curl

REST APIs use different HTTP methods for different operations. While GET retrieves and POST creates, PUT replaces, PATCH partially updates, and DELETE removes resources.

PUT Request (Full Replace)

PUT replaces the entire resource with the provided data:

curl -X PUT https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Smith", "email": "alice@example.com", "role": "admin"}'

All fields must be included in a PUT request. Omitted fields are typically set to null or their default values.

PATCH Request (Partial Update)

PATCH updates only the specified fields:

curl -X PATCH https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -d '{"role": "admin"}'

Only the role field is updated; all other fields remain unchanged.

DELETE Request

DELETE removes a resource:

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

Some APIs require a body with DELETE requests:

curl -X DELETE https://api.example.com/items \
  -H "Content-Type: application/json" \
  -d '{"ids": [1, 2, 3]}'

Idempotency

  • PUT is idempotent: calling it multiple times with the same data produces the same result
  • DELETE is idempotent: deleting the same resource twice results in the same state (resource gone)
  • PATCH may or may not be idempotent depending on the implementation

Conditional Updates with ETags

Use ETags to prevent conflicting updates:

# Get current ETag
ETAG=$(curl -sI https://api.example.com/users/42 | grep -i etag | tr -d '\r')

# Update with If-Match
curl -X PUT https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -H "If-Match: \"abc123\"" \
  -d '{"name": "Updated Name"}'

Verifying Changes

Always verify your modifications:

# Update
curl -X PATCH https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice"}' \
  -w "\nHTTP Status: %{http_code}\n"

# Verify
curl -s https://api.example.com/users/42 | jq .

A 200 or 204 response indicates success. A 409 suggests a conflict, and a 404 means the resource does not exist.

Use Case

A developer building a CRUD application needs to implement update and delete operations against a REST API, ensuring data consistency with conditional requests.

Try It — Curl to Code Converter

Open full tool