How to use Context.Request.Body and retain it?

Sanath Ballal picture Sanath Ballal · Aug 14, 2015 · Viewed 23.2k times · Source

I am trying to write a middleware where I will be doing a json schema validation against the request body. After the validation, I need to use the request body again. But I am not able to figure out how this can be done. I referred this post and found a way to access the body. But once the request body is used, I need that available to my next function.

Here is the sample code:

package main
import (
        "fmt"
        "io/ioutil"
        "net/http"
        "github.com/gin-gonic/gin"
        //"github.com/xeipuuv/gojsonschema"
)

func middleware() gin.HandlerFunc {
 return func(c *gin.Context) {
    //Will be doing json schema validation here

    body := c.Request.Body
    x, _ := ioutil.ReadAll(body)

    fmt.Printf("%s \n", string(x))

    fmt.Println("I am a middleware for json schema validation")

    c.Next()
    return
 }
}    

type E struct {
 Email    string
 Password string
}

func test(c *gin.Context) {
 //data := &E{}
 //c.Bind(data)
 //fmt.Println(data)   //prints empty as json body is already used
 
 body := c.Request.Body
 x, _ := ioutil.ReadAll(body)

 fmt.Printf("body is: %s \n", string(x))
 c.JSON(http.StatusOK, c)
}

func main() {
 router := gin.Default()

 router.Use(middleware())

 router.POST("/test", test)

 //Listen and serve
 router.Run("127.0.0.1:8080")
}

Current output:

{
    "email": "[email protected]",
    "password": "123"
} 

I am a middleware for json schema validation
body is: 

Expected output:

{
    "email": "[email protected]",
    "password": "123"
} 
I am a middleware for json schema validation
body is: {
    "email": "[email protected]",
    "password": "123"
}

Answer

transistor picture transistor · Nov 15, 2017

What Thellimist said, but in more words.

You need to "catch and restore" the Body. The Body is a buffer, that means that once you read it, its gone. So, if you catch it and "put it back", you can access it again.

Check this answer, I think this is what you are looking for: https://stackoverflow.com/a/47295689/3521313