Core API HTTP Methods

HTTP methods define the type of operation a client wants to perform. A Web API uses these methods to build a clean contract — every URL combined with an HTTP method represents a specific action on a resource. This is the foundation of REST (Representational State Transfer) design.

What Is REST?

REST is a set of rules for designing Web APIs. One of its core rules is: use the correct HTTP method for the correct type of operation. A well-designed REST API is predictable — any developer can look at a URL and the HTTP method and immediately understand what the endpoint does.

HTTP MethodOperationBookStore Example
GETRead / RetrieveGet a list of books or one book
POSTCreateAdd a new book
PUTReplace (full update)Replace all fields of a book
PATCHPartial updateUpdate only the price of a book
DELETERemoveDelete a book

HTTP GET – Reading Data

GET is used to retrieve data. It never changes data on the server. A GET request is safe to call multiple times — the result is always the same (unless data changes through other requests).

// GET /api/books — get all books
[HttpGet]
public IActionResult GetAll()
{
    return Ok(_books);
}

// GET /api/books/1 — get one book by ID
[HttpGet("{id:int}")]
public IActionResult GetById(int id)
{
    var book = _books.FirstOrDefault(b => b.Id == id);
    if (book == null) return NotFound($"Book with Id {id} not found.");
    return Ok(book);
}

Postman test for GET all books:

Method: GET
URL:    http://localhost:5000/api/books
Result: 200 OK
[
  { "id": 1, "title": "Clean Code", "author": "Robert C. Martin", ... },
  { "id": 2, "title": "The Pragmatic Programmer", "author": "David Thomas", ... }
]

HTTP POST – Creating Data

POST is used to create a new resource. The request body contains the data for the new resource in JSON format. The API processes it and stores it.

// POST /api/books — add a new book
[HttpPost]
public IActionResult Create([FromBody] Book book)
{
    if (book == null)
        return BadRequest("Book data is required.");

    book.Id = _books.Any() ? _books.Max(b => b.Id) + 1 : 1;
    book.CreatedDate = DateTime.Now;
    _books.Add(book);

    return CreatedAtAction(nameof(GetById), new { id = book.Id }, book);
}

Postman test for POST:

Method: POST
URL:    http://localhost:5000/api/books
Headers: Content-Type: application/json
Body:
{
  "title": "Design Patterns",
  "author": "Gang of Four",
  "price": 45.00,
  "category": "Technology",
  "isAvailable": true
}

Result: 201 Created
Location: http://localhost:5000/api/books/3
{
  "id": 3,
  "title": "Design Patterns",
  "author": "Gang of Four",
  "price": 45.00,
  "category": "Technology",
  "isAvailable": true,
  "createdDate": "2024-01-15T10:30:00"
}

HTTP PUT – Full Update

PUT replaces an entire resource with the data provided in the request body. All fields must be included in the request — any field not provided is treated as empty or default.

// PUT /api/books/1 — replace all fields of book with Id 1
[HttpPut("{id:int}")]
public IActionResult Update(int id, [FromBody] Book updatedBook)
{
    var book = _books.FirstOrDefault(b => b.Id == id);
    if (book == null) return NotFound($"Book with Id {id} not found.");

    // Replace all fields
    book.Title = updatedBook.Title;
    book.Author = updatedBook.Author;
    book.Price = updatedBook.Price;
    book.Category = updatedBook.Category;
    book.IsAvailable = updatedBook.IsAvailable;

    return NoContent();   // 204 — success, no data to return
}

Postman test for PUT:

Method: PUT
URL:    http://localhost:5000/api/books/1
Headers: Content-Type: application/json
Body:
{
  "title": "Clean Code (Updated Edition)",
  "author": "Robert C. Martin",
  "price": 32.99,
  "category": "Technology",
  "isAvailable": false
}

Result: 204 No Content

HTTP PATCH – Partial Update

PATCH updates only the specific fields provided in the request. It is more efficient than PUT when only one or two fields need to change. ASP.NET Core supports PATCH using the JSON Patch standard with the Microsoft.AspNetCore.JsonPatch package.

// Install package: dotnet add package Microsoft.AspNetCore.JsonPatch
// Install package: dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson

// In Program.cs:
builder.Services.AddControllers().AddNewtonsoftJson();
// PATCH /api/books/1 — update only specific fields
[HttpPatch("{id:int}")]
public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<Book> patchDoc)
{
    var book = _books.FirstOrDefault(b => b.Id == id);
    if (book == null) return NotFound();

    patchDoc.ApplyTo(book, ModelState);

    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    return NoContent();
}

Postman test for PATCH (update only the price):

Method: PATCH
URL:    http://localhost:5000/api/books/1
Headers: Content-Type: application/json-patch+json
Body:
[
  { "op": "replace", "path": "/price", "value": 19.99 },
  { "op": "replace", "path": "/isAvailable", "value": true }
]

Result: 204 No Content

HTTP DELETE – Removing Data

DELETE removes a resource identified by the URL. The server processes it and returns 204 No Content on success, or 404 Not Found if the resource does not exist.

// DELETE /api/books/1 — remove book with Id 1
[HttpDelete("{id:int}")]
public IActionResult Delete(int id)
{
    var book = _books.FirstOrDefault(b => b.Id == id);
    if (book == null) return NotFound($"Book with Id {id} not found.");

    _books.Remove(book);
    return NoContent();   // 204 — deleted successfully
}

Postman test for DELETE:

Method: DELETE
URL:    http://localhost:5000/api/books/1

Result: 204 No Content

PUT vs PATCH — Which to Use?

ScenarioUse
Updating all fields of a bookPUT
Updating only the price of a bookPATCH
Marking a book as unavailablePATCH
Replacing a book with a completely new versionPUT

HTTP Status Code Reference

Status CodeNameWhen It Is Returned
200OKGET request succeeded, data returned
201CreatedPOST request succeeded, resource created
204No ContentPUT/PATCH/DELETE succeeded, no data returned
400Bad RequestInvalid data sent by client
401UnauthorizedAuthentication required
403ForbiddenAuthenticated but not allowed
404Not FoundResource does not exist
500Internal Server ErrorUnexpected server-side failure

BookStore API – Complete HTTP Method Map

Method   URL               Body Required?    Response
─────────────────────────────────────────────────────────────────────
GET      /api/books        No               200 OK + book list
GET      /api/books/{id}   No               200 OK + one book / 404
POST     /api/books        Yes (full book)  201 Created + new book
PUT      /api/books/{id}   Yes (full book)  204 No Content / 404
PATCH    /api/books/{id}   Yes (patch ops)  204 No Content / 404
DELETE   /api/books/{id}   No               204 No Content / 404

Leave a Comment