How to show reason for error 422 in GO

1

Lately I'm breaking my head to debug POST and PUT requests where the HTTP 422 (Incorrect Input Format) error always occurs

In my project I'm using the GIN framework to do a JSON BIND from the front end. I needed to know what the structure attribute is that is not in the proper format.

How do I give a PRINT of validation errors?

I've tried the following:

func postPerson(c *gin.Context) {

    var person models.Person
    c.BindJSON(&person)

    fmt.Print(c.Errors.Error())
}
    
asked by anonymous 27.12.2016 / 18:22

1 answer

0

I discovered that to show the errors is either 400 or 422 you can get it from BindJSON. It would look more like this:

func postPerson(c *gin.Context) {

    var person models.Person
    err := c.BindJSON(&person)

    if err != nil {
       fmt.Print(err.Error())
    }
}
    
12.01.2017 / 22:26