Skip to main content

Overview

The bodydump middleware captures and logs request and response bodies for debugging purposes. Use it when you need:
  • Debug API requests
  • Log request/response pairs
  • Troubleshoot integrations

Installation

Quick Start

Configuration

Options

Examples

Basic Logging

Skip Large Bodies

Skip Specific Paths

Skip Content Types

Request Only

Response Only

JSON Formatting

API Reference

Functions

Technical Details

Implementation Overview

The bodydump middleware uses a capture mechanism to intercept and store request/response bodies:
  1. Request Body Capture: Reads the request body using io.LimitReader to respect the MaxSize limit, then restores it using io.NopCloser with a bytes reader so downstream handlers can still access it.
  2. Response Body Capture: Implements a custom responseCapture wrapper that implements http.ResponseWriter. It intercepts Write() calls to capture the response body up to the configured MaxSize while still writing to the original response writer.
  3. Filtering Mechanisms:
    • Path Filtering: Uses a map lookup for O(1) skip path checks
    • Content-Type Filtering: Skips configured content types to avoid dumping binary data
    • Size Limiting: Enforces maximum capture size on both request (via io.LimitReader) and response (via byte counting)

Default Values

  • MaxSize: 64KB (65,536 bytes)
  • Request: true (enabled)
  • Response: true (enabled)

Performance Considerations

  • Path and content-type skipping uses hash maps for efficient lookups
  • Body capture is limited by MaxSize to prevent memory issues
  • Request body is read once and restored for handler use
  • Response capture adds minimal overhead as it writes through to the original writer

Security Warning

Body dump may capture sensitive data. Never use in production without proper filtering.

Best Practices

  • Only enable in development/debugging
  • Filter sensitive data
  • Set reasonable size limits
  • Skip high-volume endpoints

Testing

The bodydump middleware includes comprehensive test coverage for all major functionality: