Using LogFormatterParams.Request.Body to read c fails, using ctx.Request.Body in *gin.Context works fine

QQ_1726416616521

Comment From: JimChenWYU

// LoggerWithConfig instance a Logger middleware with config.
func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
    formatter := conf.Formatter
    if formatter == nil {
        formatter = defaultLogFormatter
    }

        ...

    return func(c *Context) {
        // Start timer
        start := time.Now()
        path := c.Request.URL.Path
        raw := c.Request.URL.RawQuery

        // Process request
        c.Next()

                 ...

        fmt.Fprint(out, formatter(param))
    }
}

If you has been read request.Body in your handler, after in logformat middleware will be none.


like this

package main

import (
    "fmt"
    "io"

    "github.com/gin-gonic/gin"
)

func main() {
    g := gin.Default()
    g.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
        rc := io.NopCloser(params.Request.Body)
        b, err := io.ReadAll(rc)
        fmt.Println(string(b), err)   // none, because `/test` has been read `Body`.
        return ""
    }))
    g.POST("test", func(c *gin.Context) {
        b, _ := io.ReadAll(c.Request.Body)
        c.String(200, "body: %s", string(b))
    })
    g.Run(":8080")
}