Why REST?

Why using REST?

REST, or Representational State Transfer, is a software architectural style that defines a set of constraints to be used for creating Web services. REST is based on the HTTP protocol, and it uses HTTP methods to manipulate data stored on a server.

There are several reasons why you should consider using REST in your HTTP API - based on your use case:

  • REST gives you a strict paradigm of what an API should look like
  • REST is widely adopted: REST has become the de facto standard for building APIs, and it is supported by a wide range of client libraries and tools. This makes it easy for developers to use and interact with your API, as there is a wealth of resources available for learning how to use REST.
  • REST is flexible: REST does not prescribe a specific way of implementing an API, and it allows you to design your API in a way that best fits your needs. This means that you can choose the right data formats, authentication methods, and other features for your API, making it easier to build and maintain.
  • REST is scalable: REST APIs are designed to be scalable, and they can handle a large number of requests without experiencing performance issues. This is important if you expect your API to be used by a large number of users or if you expect to add more features to your API over time.

Here is an example of a simple REST API implemented using ASP.NET Core and JSON:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace MyAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CustomersController : ControllerBase
    {
        // GET api/customers
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "customer1", "customer2" };
        }

        // GET api/customers/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "customer" + id;
        }

        // POST api/customers
        [HttpPost]
        public void Post([FromBody] string value)
        {
            // Add a new customer
        }

        // PUT api/customers/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
            // Update an existing customer
        }

        // DELETE api/customers/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            // Delete a customer
        }
    }
}

In this example, the CustomersController class defines a set of actions for interacting with customer data. The [Route("api/[controller]")]attribute specifies the base URL for the API, and the[HttpGet], [HttpPost], [HttpPut], and [HttpDelete] attributes indicate which HTTP methods are supported for each action.

The Get action returns a list of customer names, and the Get(int id) action returns the name of a specific customer based on the id parameter. The Post action adds a new customer, the Put action updates an existing customer, and the Delete action removes a customer.

The limits of REST

While REST is a widely adopted and effective architectural style for building APIs, it is not suitable for every use case. Here are some potential disadvantages of using REST:

  • REST is a paradigm designed primarily for querying and manipulating data structures. However, actions (e.g. uploading an image, sending an email, ping) are not part of the REST paradigm (e.g. the URL structures).

  • REST can be very complex to implement: While REST is flexible and allows you to design your API in a way that best fits your needs, this can also make it more complex to implement. You need to consider factors such as data formats, authentication methods, and error handling, which can be time-consuming and require a lot of planning.

  • REST does not provide real-time communication: REST APIs are designed for request-response communication, meaning that the client sends a request to the server and waits for a response. This means that REST is not well-suited for use cases that require real-time communication, such as chat applications or online gaming.

  • REST is not always the most efficient: REST APIs are typically built around the CRUD (create, read, update, delete) model, which can be inefficient for certain use cases. For example, if you need to retrieve a large amount of data from a server, it may be more efficient to use a different communication protocol such as WebSockets or Server-Sent Events.