Question: I'm using GORM and the Gin Web Framework to create a RESTful API. I'm trying to bind the body of a JSON request with BindJSON to the model, but the model contains a slice of byte leading to an error. How can I bind the body to the model without changing the datatype to string?
Model:
type User struct {
Password []byte `json:"password" binding:"required"`
}
Request Body:
{
"password": "mypassword"
}
Controller:
r.PUT("/user", func(c *gin.Context) {
var user User
err := c.BindJSON(&user)
if err != nil {
fmt.Println(err)
return
}
})
Error: illegal base64 data at input byte 12
This error appears, as Gin tries to bind a string (from the JSON body) to a []byte (from the model) and fails.
Solution that I don't like:
I know that the solution could be to create a different struct with the Password as type string like following and add it to the controller, so that BindJSON works properly:
type user struct {
Password string `json:"password" binding:"required"`
}
This solution does not reflect my model and I need to add extra code. How can I adapt my model/context binding?