Skip to main content

Overview

The transformer middleware transforms request and response data, enabling format conversion, field mapping, and data manipulation. Use it when you need:
  • API version compatibility
  • Field renaming/mapping
  • Format conversion

Installation

Quick Start

Configuration

Options

Examples

Request Transformation

Response Transformation

Field Mapping

Version Compatibility

API Reference

Functions

Technical Details

The transformer middleware provides a flexible architecture for modifying HTTP requests and responses:

Request Transformation

Request transformers are applied sequentially before the handler executes. Each transformer receives the *http.Request object and can modify:
  • Headers (add, set, remove)
  • URL path and query parameters
  • Request body content
The middleware supports multiple request transformers, applied in the order they are registered.

Response Transformation

Response transformation uses a custom responseRecorder that captures:
  • HTTP status code (defaults to 200 OK if not set)
  • Response headers
  • Response body bytes
The recorder intercepts all response writes, allowing transformers to modify the complete response before it’s sent to the client. Response transformers are applied sequentially and can modify:
  • Status codes (mapping or conditional changes)
  • Headers (add, set, remove)
  • Body content (transformation or replacement)

Implementation Details

  • Request transformers receive *http.Request and return an error
  • Response transformers receive (statusCode int, headers http.Header, body []byte) and return (int, http.Header, []byte, error)
  • The middleware restores the original response writer after capturing to ensure proper cleanup
  • Headers from the recorder are copied to the original writer before writing the status and body
  • Empty response bodies are handled gracefully

Best Practices

  • Use for API version compatibility
  • Keep transformations simple
  • Document transformation rules
  • Test edge cases thoroughly

Testing

The transformer middleware includes comprehensive test coverage for all transformation functions: