|
| 1 | +--- |
| 2 | +title: Body Dump |
| 3 | +description: Capture payloads de request e response e passe-os para um handler para logging ou debugging. |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +O middleware Body Dump captura os payloads de request e response e os passa para um |
| 9 | +handler registrado. Em geral, ele é usado para debugging ou logging. |
| 10 | + |
| 11 | +:::caution |
| 12 | +Evite Body Dump para payloads grandes, como uploads ou downloads de arquivos. Se precisar usá-lo |
| 13 | +nessas rotas, adicione uma exceção na função skipper. |
| 14 | +::: |
| 15 | + |
| 16 | +## Uso |
| 17 | + |
| 18 | +```go |
| 19 | +e := echo.New() |
| 20 | +e.Use(middleware.BodyDump(func(c *echo.Context, reqBody, resBody []byte, err error) { |
| 21 | + // Handle the request and response bodies. |
| 22 | +})) |
| 23 | +``` |
| 24 | + |
| 25 | +## Configuração customizada |
| 26 | + |
| 27 | +```go |
| 28 | +e := echo.New() |
| 29 | +e.Use(middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{})) |
| 30 | +``` |
| 31 | + |
| 32 | +## Configuração |
| 33 | + |
| 34 | +```go |
| 35 | +type BodyDumpConfig struct { |
| 36 | + // Skipper defines a function to skip middleware. |
| 37 | + Skipper Skipper |
| 38 | + |
| 39 | + // Handler receives the request and response payloads and the handler error, if any. |
| 40 | + // Required. |
| 41 | + Handler BodyDumpHandler |
| 42 | + |
| 43 | + // MaxRequestBytes limits how much of the request body to dump. If the request body |
| 44 | + // exceeds this limit, only the first MaxRequestBytes are dumped and the handler |
| 45 | + // receives truncated data. |
| 46 | + // Default: 5 * MB (5,242,880 bytes). Set to -1 to disable limits (not recommended). |
| 47 | + MaxRequestBytes int64 |
| 48 | + |
| 49 | + // MaxResponseBytes limits how much of the response body to dump. If the response body |
| 50 | + // exceeds this limit, only the first MaxResponseBytes are dumped and the handler |
| 51 | + // receives truncated data. |
| 52 | + // Default: 5 * MB (5,242,880 bytes). Set to -1 to disable limits (not recommended). |
| 53 | + MaxResponseBytes int64 |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +O `Handler` tem a assinatura: |
| 58 | + |
| 59 | +```go |
| 60 | +type BodyDumpHandler func(c *echo.Context, reqBody []byte, resBody []byte, err error) |
| 61 | +``` |
| 62 | + |
| 63 | +### Configuração padrão |
| 64 | + |
| 65 | +```go |
| 66 | +// Effective defaults applied when fields are left unset (Handler is required). |
| 67 | +BodyDumpConfig{ |
| 68 | + Skipper: DefaultSkipper, |
| 69 | + MaxRequestBytes: 5 * MB, |
| 70 | + MaxResponseBytes: 5 * MB, |
| 71 | +} |
| 72 | +``` |
0 commit comments