REST API Fundamentals

Fundamental concepts and building blocks of REST APIs in the .NET ecosystem

This section covers the core foundations of RESTful API design and development, providing essential knowledge for building effective, scalable, and maintainable REST services.

Overview

REST (Representational State Transfer) is an architectural style that defines a set of constraints for creating web services. RESTful APIs use standard HTTP methods, leverage the existing features of the protocol, and focus on resources rather than operations.

HTTP Foundation

REST APIs are built on top of HTTP (Hypertext Transfer Protocol), utilizing its methods, status codes, and headers to enable communication between clients and servers:

HTTP MethodTypical Usage in REST APIsIdempotentSafe
GETRetrieve a resource or collectionYesYes
POSTCreate a new resourceNoNo
PUTUpdate a resource (complete replacement)YesNo
PATCHUpdate a resource (partial modification)Yes*No
DELETERemove a resourceYesNo
HEADRetrieve headers only (like GET without body)YesYes
OPTIONSGet supported methods/CORS preflightYesYes

*PATCH is idempotent only if properly implemented

Key HTTP Components for RESTful Design

  • HTTP Headers: Metadata about the request/response (Content-Type, Accept, Authorization)
  • Status Codes: Numerical indicators of request outcome (200 OK, 404 Not Found, 500 Server Error)
  • Content Negotiation: Mechanism for serving different representations of a resource
  • URI Path & Query Parameters: Identify resources and filter/sort results

Core REST Concepts

REST is built around several key concepts that guide API design:

Resources

In REST, everything is a resource that can be identified by a unique URI. Resources can be:

  • Documents: Single resources (e.g., a specific user)
  • Collections: Groups of resources (e.g., all users)
  • Store: Client-managed resource collections
  • Controller: Procedure-like resources that don’t fit the CRUD model

Representations

Resources can have multiple representations (JSON, XML, etc.) based on client needs. Content negotiation determines which representation is served in the response.

Statelessness

Each request from client to server must contain all information needed to understand and process the request. The server shouldn’t store client context between requests.

Uniform Interface

RESTful APIs provide a uniform way to interact with resources using standard HTTP methods, self-descriptive messages, and hypermedia as the engine of application state (HATEOAS).

RESTful Principles

The following principles guide the design of truly RESTful APIs:

1. Resource-Based

APIs should be organized around resources, with resource identifiers (URIs) used consistently.

2. HTTP Method Semantics

Use HTTP methods according to their defined semantics:

  • GET for retrieving data
  • POST for creating resources
  • PUT for replacing resources
  • PATCH for partial updates
  • DELETE for removing resources

3. Appropriate Status Codes

Return meaningful HTTP status codes that properly convey the outcome of operations.

4. HATEOAS (Hypermedia as the Engine of Application State)

APIs should include links in responses that guide clients on possible next actions, reducing the need for clients to have URI construction knowledge.

5. Layered System

API architecture can include multiple layers (load balancers, gateways, etc.) that are invisible to clients.

Pros and Cons of REST API Approaches

Below is a table of pros and cons for various aspects of REST API implementation:

ApproachProsCons
REST vs. GraphQL• Caching support
• Standardized methods
• Widely adopted
• Statelessness improves scalability
• Multiple requests for complex data
• Potential over-fetching/under-fetching
• Less flexible for varied client needs
REST vs. gRPC• Human-readable formats
• Easier debugging
• Broad browser support
• Less client/server coupling
• Typically slower performance
• Larger payload sizes
• No built-in code generation
• No built-in streaming
Resource-based design• Intuitive organization
• Clear mapping to domain objects
• Easier to understand and document
• May not fit all operations
• Can be challenging for complex relationships
• May lead to endpoint proliferation
JSON representation• Lightweight and readable
• Native JavaScript support
• Wide tool support
• No schema requirement
• No built-in schema validation
• Type information needs external definition
• Verbosity compared to binary formats
XML representation• Built-in validation (XSD)
• Rich metadata support
• Namespaces
• Mature tooling
• Verbose
• More processing overhead
• More complex to parse
• Less popular in modern APIs
REST versioning in URL• Explicit and visible
• Easy client implementation
• Clear version isolation
• Breaks resource identification purity
• Complicates URI construction
• Can lead to code duplication
REST versioning in header• Maintains clean URIs
• Preserves resource identification
• More RESTful approach
• Less visible
• More complex client implementation
• Harder to test in browsers
Strict REST compliance• More predictable behavior
• Better alignment with HTTP
• Improved cacheability
• More self-documenting
• Implementation complexity
• HATEOAS adds overhead
• May be overkill for simple APIs
• Steeper learning curve
Pragmatic REST• Faster implementation
• Lower learning barrier
• More flexibility
• Focus on practical needs
• Less consistent across implementations
• May sacrifice beneficial constraints
• Potential for anti-patterns

REST Maturity Model (Richardson Maturity Model)

REST implementation can be evaluated according to the Richardson Maturity Model:

Level 0: Swamp of POX

  • Single URI for all operations
  • One HTTP method (typically POST)
  • Example: SOAP over HTTP

Level 1: Resources

  • Multiple URIs for different resources
  • Still using a single HTTP method

Level 2: HTTP Verbs

  • Uses different HTTP methods appropriately
  • Returns standard status codes
  • Most common level for “RESTful” APIs

Level 3: Hypermedia Controls (HATEOAS)

  • Responses include links to related resources
  • Clients don’t need to know URIs in advance
  • Fully self-discoverable API

Most real-world “RESTful” APIs operate at Level 2, with few reaching the full Level 3 hypermedia implementation.


Understanding REST Fundamentals

Fundamental concepts and principles that define RESTful API architecture

API Contracts and Documentation Standards

Understanding and implementing API contracts for REST services using OpenAPI, Swagger, and other tools