Best Practices in REST API: Exploring HTTP Verbs with C# Examples

Introduction

Hello, devs! REST APIs are the backbone of modern web communication.

In this post, we will explore best practices for creating RESTful APIs and understand the differences between HTTP verbs with practical examples in C#.

We’ll use an order and item structure as examples to make everything more practical and applicable to your daily development.

Let’s go?

REST API

What is a REST API?

A REST API (Representational State Transfer) is a set of rules that allows communication between systems using the HTTP protocol.

They are widely used due to their simplicity and ability to integrate different systems efficiently.

Key characteristics of a RESTful API:

Best Practices for REST APIs

Endpoint Design:

API Versioning:

Authentication and Authorization:

Error Handling:

HTTP Verbs and Their Uses

GET: Retrieving data

POST: Creating new resources

PUT: Updating existing resources

PATCH: Partial update of resources

DELETE: Removing resources

Code Examples in C#

Let’s examine a C# code example that demonstrates how to use a REST API.

[ApiController]
[Route("api/v1/orders")]
public class OrdersController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> GetOrders()
    {
        // Call logic to retrieve orders
        return Ok(/* list of orders */);
    }

    [HttpPost]
    public async Task<IActionResult> CreateOrder([FromBody] Order order)
    {
        // Call logic to create order
        return CreatedAtAction(nameof(GetOrderById), new { id = order.Id }, order);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetOrderById(int id)
    {
        // Call logic to retrieve order by ID
        return Ok(/* order */);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> UpdateOrder(int id, [FromBody] Order updatedOrder)
    {
        if (id != updatedOrder.Id)
        {
            return BadRequest();
        }

        // Call logic to update order

        return NoContent();
    }
}
[ApiController]
[Route("api/v1/orders/{orderId}/items")]
public class ItemsController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> GetItems(int orderId)
    {
        // Call logic to retrieve order items
        return Ok(/* list of items */);
    }

    [HttpPatch("{itemId}")]
    public async Task<IActionResult> UpdateItemPartial(int orderId, int itemId, [FromBody] JsonPatchDocument<Item> patchDoc)
    {
        if (patchDoc == null)
        {
            return BadRequest();
        }

        // Call logic to partially update item

        return NoContent();
    }

    [HttpDelete("{itemId}")]
    public async Task<IActionResult> DeleteItem(int orderId, int itemId)
    {
        // Call logic to delete item

        return NoContent();
    }
}

Conclusion

Following best practices for developing RESTful APIs is essential to create robust and efficient systems.

Understanding and correctly using HTTP verbs, along with a good code structure, will ensure your API is easy to maintain and scale.

We hope these C# examples help you implement your own APIs effectively.

If you have any questions or suggestions, leave a comment below!

See ya!