HTTP Methods Explained: GET, POST, PUT, DELETE
HTTP methods form the foundational verbs of web communication, dictating how clients interact with servers and databases. Building upon previous concepts of requests, responses, headers, bodies, and status codes, this chapter explores how these methods enable full Create, Read, Update, and Delete (CRUD) functionality across distributed systems.
1. The Role of HTTP Methods in System Design
When a client communicates with a web server over the internet, it needs a standardized way to express what action it wants to perform on a resource. HTTP methods provide this shared vocabulary.
- Beyond Simple Reading: HTTP is not just for fetching static web pages or rendering UI templates; it is an interactive protocol that instructs servers to manipulate data in underlying databases.
- The CRUD Mapping: HTTP methods map directly to core database operations:
- Create: Adding new records (
POST). - Read: Fetching existing records (
GET). - Update: Modifying existing records (
PUT/PATCH). - Delete: Removing records (
DELETE).
- Create: Adding new records (
Client (Browser / App)
|
| sends HTTP Request with Method (GET, POST, PUT, DELETE) + URL + Optional Body
v
Web Server
|
| translates method into SQL/ORM query
v
Database (Tables: users, products)
2. Reading Data with the GET Method
The GET method is used when a client wants to request and retrieve information from a server. Under the hood, the server queries its database and returns the requested payload in the HTTP response body.
Fetching Collections vs. Specific Resources
Databases typically organize data into tables, such as a products table containing multiple items.
- Fetching All Records: If a user requests all items, the server queries the entire table and returns an array of objects.
- Endpoint:
GET /products - Response: A collection of all product objects.
- Endpoint:
- The Scalability Problem: As tables grow to millions of rows, fetching every single record becomes inefficient, slow, and resource-heavy. Clients rarely need all information at once.
Fetching by ID (GET by ID)
To retrieve a specific record without fetching the entire table, clients append a unique identifier to the URL path.
3. Creating Data with the POST Method
When a user wants to add new information to the system—such as registering a new user or adding a new item to a product catalog—they use the POST method.
- Request Payload: Unlike
GET, which typically relies on URL parameters or query strings, aPOSTrequest carries data inside the HTTP request body. - Server Responsibility: The server receives the object, validates it, and executes an insertion query against the database.
Example Workflow: Adding a Product
- Client Action: Sends a
POSTrequest to/productswith a JSON body representing the new item.JSON{ "id": 3, "name": "refrigerator" } - Server Action: Intercepts the request, parses the body, and inserts a new row into the
productstable. - Database State: The table now contains three distinct entries (IDs 1, 2, and 3).
4. Updating Data with the PUT Method
When existing information in the database needs to be modified, developers utilize the PUT method.
- Targeting Resources: Similar to fetching a specific record,
PUTtargets a resource by its unique identifier in the URL path (e.g.,/products/3). - Complete Replacement: The client sends a completely new representation of the object in the request body. The server replaces the existing database record with this newly provided object.
Example Workflow: Updating via PUT
- Endpoint:
PUT /products/3 - Request Body:
JSON
{ "id": 3, "name": "washing machine" } - Result: The server updates product ID 3, changing its name from "refrigerator" to "washing machine".
5. Efficient Updates with the PATCH Method
While PUT is powerful, it has efficiency and safety drawbacks in production environments, leading to the use of an alternative method: PATCH.
The Problem with PUT
If an object contains numerous fields (e.g., description, manufacturing year, last service date, dimensions, warranty status), using PUT requires transmitting the entire object back to the server, even if only a single field is changing.
PUT Request: Must send ALL fields (Risk of overwriting unchanged data with typos or stale values)
PATCH Request: Sends ONLY the specific fields that need modification
Comparing PUT vs. PATCH
6. Removing Data with the DELETE Method
When information is no longer needed, the DELETE method allows clients to remove records from the database.
- Targeted Deletion: The client sends a
DELETErequest directed at the specific resource endpoint. - Endpoint Structure:
DELETE /products/3instructs the server to remove the product matching ID 3 from theproductstable. - Server Execution: The server validates permissions, executes a deletion query against the database, and returns a success status code (such as
200 OKor204 No Content) indicating the resource has been purged.
7. Summary of HTTP Methods in CRUD Operations
The following table synthesizes how HTTP methods map directly to standard database operations and URL conventions:
Summary and Key Takeaways
- HTTP as an Interactive Interface: HTTP is not restricted to viewing static pages; it empowers clients to perform full CRUD operations on server-side databases.
GETfor Retrieval: Used to fetch either an entire collection of items or a specific resource using an identifier (e.g.,/products/2).POSTfor Creation: Relies on transmitting a request body to add brand-new records to server collections.PUTfor Full Replacement: Updates an existing record by sending its complete, updated object representation to a specific resource path.PATCHfor Efficiency: Optimizes updates by sending only the delta fields that require modification, minimizing bandwidth and accidental overwrite risks.DELETEfor Removal: Targets specific resource identifiers to permanently purge records from the database.- Foundation of REST APIs: Mastery of these HTTP methods serves as the fundamental stepping stone for understanding and designing robust RESTful APIs in modern system architecture.