Skip to main content

Overview

The hypermedia middleware provides helpers for building HATEOAS (Hypermedia as the Engine of Application State) APIs with links and embedded resources. Use it when you need:
  • RESTful hypermedia APIs
  • Self-documenting responses
  • Discoverable APIs

Installation

Quick Start

Configuration

Options

Examples

HAL Format

Embedded Resources

Collection with Pagination

API Reference

Functions

Technical Details

Architecture

The hypermedia middleware uses a response recorder pattern to intercept and modify JSON responses:
  1. Context Storage: Links are stored in the request context using a private context key
  2. Response Recording: A custom responseRecorder captures the response body and status code
  3. JSON Modification: The middleware parses the JSON response, injects links, and re-encodes it
  4. Content-Type Filtering: Only processes responses with application/json content type

Implementation Details

Link Structure:
  • Href: The URL of the linked resource
  • Rel: The relationship type (e.g., “self”, “next”, “prev”)
  • Method: Optional HTTP method for the link
  • Title: Optional human-readable description
  • Type: Optional media type hint
Response Recorder: The middleware implements a custom responseRecorder that wraps the original http.ResponseWriter to:
  • Capture response body in a buffer
  • Record status code
  • Allow modification before final write
HAL+JSON Support: The HAL type provides full HAL+JSON specification support with:
  • Properties for resource data
  • Links map for hypermedia links
  • Embedded map for nested resources
  • Custom JSON marshaling to flatten properties into the root object

Performance Considerations

  • Links are stored as pointers in context to avoid copying
  • JSON parsing only occurs for application/json responses
  • Non-JSON responses pass through without modification
  • Response buffering adds minimal overhead

Security Considerations

  • Base URL validation prevents injection attacks
  • Links are added server-side, not from user input
  • TLS detection for automatic scheme selection
  • No sensitive data should be exposed in link URLs

Best Practices

  • Always include self link
  • Use consistent link relations
  • Document link relations
  • Consider client library support

Testing

The hypermedia middleware includes comprehensive test coverage for all functionality: