HTTP Methods Explainer
Complete guide to HTTP request methods with examples and use cases.
π Quick Reference
| Method | Safe | Idempotent | Body | Purpose |
|---|---|---|---|---|
| GET | β | β | β | Retrieve a resource |
| POST | β | β | β | Create a new resource |
| PUT | β | β | β | Replace/update entire resource |
| PATCH | β | β | β | Partial resource update |
| DELETE | β | β | β | Remove a resource |
| HEAD | β | β | β | Get headers only (no body) |
| OPTIONS | β | β | β | Get allowed methods |
| TRACE | β | β | β | Echo request for debugging |
| CONNECT | β | β | β | Establish tunnel |
Understanding HTTP Methods
HTTP methods (also called HTTP verbs) define the action to be performed on a resource. Each method has specific semantics that determine how servers should handle the request. Understanding these methods is fundamental for API development and web programming.
This guide covers all standard HTTP methods from the commonly used GET, POST, PUT, DELETE to the less common TRACE and CONNECT. Each method has its purpose and proper usage in RESTful API design.
Safe Methods
Safe methods (GET, HEAD, OPTIONS, TRACE) should not modify server state. They are read-only operations. Calling them multiple times has no side effects. Browsers can prefetch safe resources without concern.
Idempotent Methods
Idempotent methods (GET, PUT, DELETE, HEAD, OPTIONS, TRACE) produce the same result regardless of how many times they're called. PUT updating a resource to the same value is still idempotent. POST is not idempotentβeach call may create a new resource.
RESTful API Design
Use GET to retrieve resources. POST to create new resources. PUT for full updates. PATCH for partial updates. DELETE to remove resources. This consistent mapping makes APIs intuitive and predictable.
CORS and Preflight
OPTIONS requests are used in CORS preflight to check if the actual request is allowed. Understanding this is crucial when building APIs that serve browser clients from different origins.