请求体转储
Body Dump 中间件会捕获请求和响应负载,并传给已注册的处理函数。它通常用于调试或日志记录。
所有核心中间件都位于 middleware 包中:
import "github.com/labstack/echo/v5/middleware"e := echo.New()e.Use(middleware.BodyDump(func(c *echo.Context, reqBody, resBody []byte, err error) { // Handle the request and response bodies.}))e := echo.New()e.Use(middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{}))BodyDumpConfig · github.com/labstack/echo/v5@5196b9b
字段说明由英文源码注释生成。
| 字段 | 类型 | 说明 | 源码 |
|---|---|---|---|
Skipper | Skipper | Skipper defines a function to skip middleware. | L21 |
Handler | BodyDumpHandler | Handler receives request, response payloads and handler error if there are any. Required. | L25 |
MaxRequestBytes | int64 | MaxRequestBytes limits how much of the request body to dump. If the request body exceeds this limit, only the first MaxRequestBytes are dumped. The handler callback receives truncated data. Default: 5 * MB (5,242,880 bytes) Set to -1 to disable limits (not recommended in production). | L32 |
MaxResponseBytes | int64 | MaxResponseBytes limits how much of the response body to dump. If the response body exceeds this limit, only the first MaxResponseBytes are dumped. The handler callback receives truncated data. Default: 5 * MB (5,242,880 bytes) Set to -1 to disable limits (not recommended in production). | L39 |
Handler 的签名为:
type BodyDumpHandler func(c *echo.Context, reqBody []byte, resBody []byte, err error)// Effective defaults applied when fields are left unset (Handler is required).BodyDumpConfig{ Skipper: DefaultSkipper, MaxRequestBytes: 5 * MB, MaxResponseBytes: 5 * MB,}