Overview
Thebodydump 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:-
Request Body Capture: Reads the request body using
io.LimitReaderto respect theMaxSizelimit, then restores it usingio.NopCloserwith a bytes reader so downstream handlers can still access it. -
Response Body Capture: Implements a custom
responseCapturewrapper that implementshttp.ResponseWriter. It interceptsWrite()calls to capture the response body up to the configuredMaxSizewhile still writing to the original response writer. -
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
MaxSizeto 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:Related Middlewares
- logger - Request logging
- audit - Audit logging
- requestlog - Detailed logging