REST & HTTP – REST APIs
Before you begin: Join our book community on Discord
https://packt.link/EarlyAccess
This chapter delves into the heart of web application communication–REST APIs. In today’s connected digital world, effective communication between different applications is paramount, and RESTful APIs play a pivotal role in facilitating this interaction.We start by exploring the basic fabric of the web: the HTTP protocol. We touch on the core HTTP methods such as GET, POST, PUT, and DELETE to see how they carry out CRUD (Create, Read, Update, Delete) operations in a RESTful context. We then turn our attention to HTTP status codes–the system’s way of informing clients about the status of their requests–and HTTP headers.Since APIs evolve and managing these changes without disrupting existing clients is a significant challenge, we look at different strategies for API versioning and the trade-offs involved with each.Then we learn about the Data-Transfer Object (DTO) pattern. Packaging data into DTOs can provide many benefits, from reducing the number of calls to better encapsulation and improved performance when sending data over the network.Finally, we also explore the importance of defining clear and robust API contracts, which ensures API stability. We discuss techniques for designing and documenting these contracts, ensuring they serve as practical guides for API consumers.By the end of this chapter, you’ll know how REST APIs work and will be ready to start building some using ASP.NET Core as we move further into our architectural journey in the next few chapters.In this chapter, we cover the following topics:
- REST & HTTP
- Data Transfer Object (DTO)
- API contracts
REST, or Representational State Transfer, is a way to create internet-based services, known as web services, web APIs, REST APIs, or RESTful APIs. Those services commonly use HTTP as their transport protocol. REST reuses well-known HTTP specifications instead of recreating new ways of exchanging data. For example, returning an HTTP status code 200 OK indicates success, while 400 Bad Request indicates failure.Here are some defining characteristics:
- Statelessness: In a RESTful system, every client-to-server request should contain all the details necessary for the server to comprehend and execute it. The server retains no information about the client’s most recent HTTP request. This enhances both reliability and scalability.
- Caching capabilities: Clients should be able to cache responses to enhance performance.
- Simplicity and lose coupling: REST uses HTTP to ensure a simplified, decoupled architecture. This makes the development, maintenance, and scaling of REST APIs easier and facilitates their usage.
- Resource identifiability: Each REST API endpoint is a distinct resource, enabling us to secure each piece of the system separately.
- Interface as a contract: The REST API layer serves as an exchange contract or an abstraction. It effectively conceals the backend system’s underlying implementation, fostering streamlined interactions.
While we could delve much deeper into the intricacies of REST APIs, the preceding characteristics serve as foundational knowledge, providing good enough knowledge to get started with RESTful services. Having navigated through these essentials, let’s shift our focus toward understanding how REST APIs harness the power of HTTP.