The problem is that you are printing the string value c.Request.Body , which has the ReadCloser interface ReadCloser .
What you can do to make sure that it really contains the body you want is to read the value from c.Request.Body for the string and then print it. This is just for your learning process!
Training Code:
func events(c *gin.Context) { x, _ := ioutil.ReadAll(c.Request.Body) fmt.Printf("%s", string(x)) c.JSON(http.StatusOK, c) }
However, you should not access the request body. Let gin do a body parsing for you using the binding.
More correct code:
type E struct { Events string } func events(c *gin.Context) { data := &E{} c.Bind(data) fmt.Println(data) c.JSON(http.StatusOK, c) }
This is a more correct way to access the data in the body, since it will already be disassembled for you. Please note: if you first read the body, as we did above in the training phase, then c.Request.Body will be empty and there will be nothing left in Gin to read Gin.
Broken code:
func events(c *gin.Context) { x, _ := ioutil.ReadAll(c.Request.Body) fmt.Printf("%s", string(x)) data := &E{} c.Bind(data)
You are probably also curious why JSON returned from this endpoint and the empty Request.Body . This is for the same reason. The JSON Marshalling method cannot serialize ReadCloser , and therefore it appears as empty.
Danver braganza
source share