Web APIs allow applications to communicate using HTTP methods. Each HTTP method has a specific purpose, especially when working with data.
Think of an API like a restaurant system:
These actions map directly to HTTP methods.
Common HTTP Methods
| Method | Purpose | Real-World Meaning |
|---|
| GET | Retrieve data | View information |
| POST | Create new data | Add new information |
| PUT | Update entire data | Replace existing information |
| PATCH | Update part of data | Modify specific fields |
Example Scenario: Product Management API
We manage products in an online store.
Product Model
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Initial Data
[
{ "id": 1, "name": "Laptop", "price": 1200 },
{ "id": 2, "name": "Mouse", "price": 25 }
]
1. GET – Retrieve Data
Purpose
Used to read data from the server.
Example
Get all products.
API Code
[HttpGet]
public IActionResult GetProducts()
{
return Ok(products);
}
Input (Request)
GET /api/products
Output (Response)
[
{ "id": 1, "name": "Laptop", "price": 1200 },
{ "id": 2, "name": "Mouse", "price": 25 }
]
Real-life example
Opening an online store to view available products.
2. POST – Create New Data
Purpose
Used to add new data to the server.
Example
Add a new product.
API Code
[HttpPost]
public IActionResult AddProduct(Product product)
{
products.Add(product);
return Ok(product);
}
Input (Request Body)
{
"id": 3,
"name": "Keyboard",
"price": 45
}
Output (Response)
{
"id": 3,
"name": "Keyboard",
"price": 45
}
Real-life example
Adding a new product to your store inventory.
3. PUT – Update Entire Data
Purpose
Used to replace all fields of an existing record.
Example
Update a product completely.
API Code
[HttpPut("{id}")]
public IActionResult UpdateProduct(int id, Product updatedProduct)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null)
return NotFound();
product.Name = updatedProduct.Name;
product.Price = updatedProduct.Price;
return Ok(product);
}
Input (Request)
PUT /api/products/1
Request Body
{
"name": "Gaming Laptop",
"price": 1500
}
Output (Response)
{
"id": 1,
"name": "Gaming Laptop",
"price": 1500
}
Real-life example
Replacing all details of a product listing.
PUT expects the full object, not partial data.
4. PATCH – Update Partial Data
Purpose
Used to modify only specific fields.
Example
Update only the product price.
API Code
[HttpPatch("{id}")]
public IActionResult UpdatePrice(int id, decimal price)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product == null)
return NotFound();
product.Price = price;
return Ok(product);
}
Input (Request)
PATCH /api/products/2
Request Body
25
Output (Response)
{
"id": 2,
"name": "Mouse",
"price": 25
}
Real-life example
Changing only the price of an item during a sale.
PUT vs PATCH (Important Difference)
| PUT | PATCH |
|---|
| Updates full object | Updates partial data |
| Replaces everything | Changes specific fields |
| Safer for full updates | Better for small changes |
Summary
GET, POST, PUT, and PATCH are fundamental HTTP methods used in ASP.NET Core Web APIs for reading, creating, and updating data. GET retrieves information without changing anything on the server, POST creates new records, PUT replaces an entire existing record with new data, and PATCH updates only specific fields within a record. Understanding the differences between these methods ensures that your APIs remain predictable, efficient, and aligned with RESTful best practices.