gogo-gin

How to log or print request received with gin?


Example:

func createOrUpdateInfluencer(c *gin.Context) { }

How to print the data in the request received in my function? In my case, I am supposed to receive JSON, how to print it without knowing what it looks like?


Solution

  • Just read and print the body is ok:

    func createOrUpdateInfluencer(c *gin.Context) {
        body, _ := io.ReadAll(c.Request.Body)
        println(string(body))
    }
    

    Or if you just want to peek it in middleware, you can put it back after read:

    func createOrUpdateInfluencer(c *gin.Context) {
        body, _ := io.ReadAll(c.Request.Body)
        println(string(body))
    
        c.Request.Body = io.NopCloser(bytes.NewReader(body))
    }