How to show reason for error 422 in GO

Asked

Viewed 77 times

1

Lately I’m breaking my head too much to debug the POST and PUT requests where HTTP 422 error always occurs (Incorrect input format)

In my project I am using the GIN framework to make a JSON BIND sent from the front end. I needed to know which attribute of the structure is not in the proper format.

How do I print the validation errors?

I tried the following:

func postPerson(c *gin.Context) {

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

    fmt.Print(c.Errors.Error())
}

1 answer

0


I found that to show errors be 400 or 422 is possible to take from Bindjson. Would be more like this:

func postPerson(c *gin.Context) {

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

    if err != nil {
       fmt.Print(err.Error())
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.