1

I have an audit log middleware which trying to log details of each grpc function call. Currently I can get request method and url. But I'm not able to get the values in Post body.

Also this middleware is use to log multiple functions which contains different post body. Is this possible?

This is what I have now:

package main

func main() {
e := echo.New()
e.Use(grpcGatewayMiddleware(mux, func(c echo.Context) bool {
    return strings.HasPrefix(c.Path(), "/skip")
}))
e.Use(logMiddleware)
...
}

func Middleware(config auth.Config) echo.MiddlewareFunc {
    return func(next echo.HandlerFunc) echo.HandlerFunc {
        return func(c echo.Context) (err error) {
            message := Message {
                Method: c.Request().Method,
                Url:    c.Request().RequestURI,
                Prams:  c.Request().Form.Encode(),
            }
            log(message)
        }
    }
}

func grpcGatewayMiddleware(mux *runtime.ServeMux, skipper middleware.Skipper) echo.MiddlewareFunc {
    if skipper == nil {
        skipper = middleware.DefaultSkipper
    }
    return func(next echo.HandlerFunc) echo.HandlerFunc {
        return func(c echo.Context) error {
            if skipper(c) {
                return next(c)
            }
            mux.ServeHTTP(c.Response(), c.Request())
            return next(c)
        }
    }
}
jason135
  • 179
  • 1
  • 3
  • 18
  • Hi jason! Have you tried using string(c.Request().Body) ? – Alejandro Lorefice Aug 18 '20 at 23:55
  • @A.Lorefice I did tried c.Request().Body, but it didn't contains the post body data i'm looking for. – jason135 Aug 19 '20 at 01:10
  • @Arun so I'm trying to log info of grpc endpoints. I just edit and added the grpc middleware. I think these are all the related parts of this. – jason135 Aug 19 '20 at 01:15
  • Can you check this , is this the same issue ? https://stackoverflow.com/questions/53021416/go-echo-not-getting-post-body-from-vue – Arun Aug 19 '20 at 04:04
  • @Arun Thanks for helping, but it isn't really the same issue. Find a solution for my situation to use `middleware.BodyDumpWithConfig` to get request body. – jason135 Aug 21 '20 at 20:36

0 Answers0