In modern web development, RESTful APIs have become the standard for building scalable, stateless, and interoperable web services. At the heart of REST architecture are a set of HTTP methods — namely GET, POST, PUT, and DELETE — which define how clients interact with server resources.
In this article, you'll learn what each of these methods means, how they work, and when to use them with real-world examples using ASP.NET Core Web API.
What is REST?
REST (Representational State Transfer) is an architectural style for designing networked applications. It uses standard HTTP methods to perform CRUD operations (Create, Read, Update, Delete) on resources, which are usually represented as URLs.
A RESTful API is:
- Stateless: Each request contains all the information needed.
- Resource-based: Every piece of data is treated as a resource.
- Uses standard HTTP verbs: GET, POST, PUT, DELETE, etc.
Core RESTful HTTP Methods
Let's break down the four primary HTTP methods used in RESTful APIs:
1. GET — Read Data
Purpose: Retrieve data from the server (read-only)
Safe and idempotent: Does not change server state
Status Code: 200 OK, 404 Not Found if the resource is missing
Example:
Request:
GET /api/products/1
{ "id": 1, "name": "Laptop", "price": 1200 }
GET Request In ASP.NET Core:
[HttpGet("{id}")] public IActionResult GetProduct(int id) { var product = _repo.GetById(id); if (product == null) return NotFound(); return Ok(product); }
2. POST — Create New Resource
Purpose: Send data to the server to create a new resource
Not idempotent: Calling multiple times will create multiple resources
Status Code: 201 Created
Example:
Request:
POST /api/products Content-Type: application/json { "name": "Tablet", "price": 500 }
Response:
201 Created Location: /api/products/3
POST Request in ASP.NET Core:
[HttpPost] public IActionResult CreateProduct(Product newProduct) { _repo.Add(newProduct); return CreatedAtAction(nameof(GetProduct), new { id = newProduct.Id }, newProduct); }
3. PUT — Update Existing Resource
Purpose: Update an existing resource entirely
Idempotent: The Same request can be repeated with the same result
Status Code: 200 OK, 204 No Content if no body returned
Example:
Request:
PUT /api/products/1 Content-Type: application/json { "id": 1, "name": "Updated Laptop", "price": 1300 }
Response:
204 No Content
PUT Request in ASP.NET Core.
[HttpPut("{id}")] public IActionResult UpdateProduct(int id, Product updatedProduct) { if (id != updatedProduct.Id) return BadRequest(); var existing = _repo.GetById(id); if (existing == null) return NotFound(); _repo.Update(updatedProduct); return NoContent(); }
4. DELETE — Remove Resource
Purpose: Delete a resource by its ID
Idempotent: Deleting a non-existent item returns the same result
Status Code: 204 No Content, 404 Not Found
Example:
Request:
DELETE /api/products/1
Response:
204 No Content
DELETE Request in ASP.NET Core:
[HttpDelete("{id}")] public IActionResult DeleteProduct(int id) { var product = _repo.GetById(id); if (product == null) return NotFound(); _repo.Delete(id); return NoContent(); }
What Does Idempotent Mean in REST APIs?
Idempotent refers to an operation that can be repeated multiple times without changing the result beyond the initial application.
In the context of REST APIs and HTTP methods, an idempotent method ensures that:
"No matter how many times a client sends the same request, the result on the server remains the same."
Simple Example:
✅ DELETE /api/products/1
- First request: Deletes product with ID 1 → returns 204 No Content.
- Second request: Product already deleted → returns 404 Not Found.
💡 But the server state hasn’t changed after the second call, so DELETE is idempotent.
Understanding GET, POST, PUT, and DELETE is essential when working with RESTful APIs in ASP.NET Core or any modern backend framework. Each HTTP method serves a specific purpose and follows well-defined rules, making your API clean, maintainable, and developer-friendly.
No comments:
Post a Comment