Web API  

Understanding GET, POST, PUT, and PATCH in ASP.NET Core Web APIs

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:

  • You view the menu

  • You place an order

  • You update an order

  • You change part of an order

These actions map directly to HTTP methods.

Common HTTP Methods

MethodPurposeReal-World Meaning
GETRetrieve dataView information
POSTCreate new dataAdd new information
PUTUpdate entire dataReplace existing information
PATCHUpdate part of dataModify 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)

PUTPATCH
Updates full objectUpdates partial data
Replaces everythingChanges specific fields
Safer for full updatesBetter 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.