Working with HTTP Methods

1. Using GET Method

PropertyValue
PurposeRetrieve resource representation
SafeYes (no side effects)
IdempotentYes
CacheableYes
BodyShould not have request body
Success200 OK, 304 Not Modified
Failure404, 401, 403

Example: GET Request

GET /api/users/42 HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer eyJhbGc...

2. Using POST Method

PropertyValue
PurposeCreate resource, non-idempotent action
SafeNo
IdempotentNo (default)
CacheableOnly with explicit headers
BodyRequired (resource representation)
Success201 Created (with Location header), 200 OK, 202 Accepted

Example: POST Create User

POST /api/users HTTP/1.1
Content-Type: application/json

{"name": "Alice", "email": "alice@example.com"}

HTTP/1.1 201 Created
Location: /api/users/43
Content-Type: application/json

{"id": 43, "name": "Alice", "email": "alice@example.com"}

3. Using PUT Method

PropertyValue
PurposeFull replacement of resource (or create at known URI)
IdempotentYes (same body → same state)
BodyComplete resource representation
Success200 OK (updated), 201 Created (new), 204 No Content
Warning: PUT replaces the ENTIRE resource. Omitted fields may be set to null/default. Use PATCH for partial updates.

4. Using PATCH Method

PropertyValue
PurposePartial update
IdempotentDepends on patch semantics (JSON Merge: yes; JSON Patch: usually yes)
Body FormatJSON Merge Patch (RFC 7396), JSON Patch (RFC 6902)
Content-Typeapplication/merge-patch+json or application/json-patch+json
Success200 OK, 204 No Content

Example: JSON Merge Patch

PATCH /api/users/42 HTTP/1.1
Content-Type: application/merge-patch+json

{"email": "newalice@example.com"}

5. Using DELETE Method

PropertyValue
PurposeRemove resource
IdempotentYes (deleting deleted = same state)
BodyUsually empty
Success204 No Content (most common), 200 OK (with body), 202 Accepted (async)
RepeatReturns 404 or 204 (both valid)

6. Using HEAD Method

PropertyValue
PurposeSame as GET but returns headers only (no body)
Use CasesCheck existence, get metadata (Content-Length, ETag), validate cache
Idempotent / SafeYes / Yes
Success200 OK (headers only)

7. Using OPTIONS Method

PropertyValue
PurposeDiscover allowed methods, CORS preflight
Response HeaderAllow: GET, POST, PUT, DELETE
CORSBrowsers send before non-simple cross-origin requests

8. Understanding Safe Methods

Safe methods do not modify server state (read-only).

MethodSafeIdempotent
GETYesYes
HEADYesYes
OPTIONSYesYes
PUTNoYes
DELETENoYes
PATCHNoUsually
POSTNoNo

9. Understanding Idempotent Methods

Idempotent: N identical requests produce the same server state as 1 request.

Note: Idempotency refers to server state, not the response. A second DELETE may return 404 instead of 204; state is still the same (resource gone).
ScenarioUse Method
Network retries safeGET, PUT, DELETE
Need uniqueness guaranteePOST + Idempotency-Key header

10. Handling Method Override

For clients that only support GET/POST (older browsers, firewalls), tunnel other methods via header.

HeaderEffect
X-HTTP-Method-Override: PUTServer treats POST as PUT
X-HTTP-Method-Override: DELETEServer treats POST as DELETE
?_method=PATCH (query)Alternate (Rails-style)
Warning: Only enable for trusted clients; can bypass method-based security rules.