I have the struct below
type foos struct { Foo string `json:"foo" binding:"required"`}
and I have the following endpoint
func sendFoo(c *gin.Context) {
var json *foos
if err := c.BindJSON(&json); err != nil {
c.AbortWithStatus(400)
return
}
// Do something about json
}
when I post this JSON
{"bar":"bar bar"}
the err is always nil. I write binding required and it doesn't work. But when I change the endpoint like below,
func sendFoo(c *gin.Context) {
var json foos //remove pointer
if err := c.BindJSON(&json); err != nil {
c.AbortWithStatus(400)
return
}
// Do something about json
}
binding works and the err is not nil. Why?