I'm trying to build a login API
type LoginRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
}
func LoginHandler(app *config.Config) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
var loginRequest LoginRequest
if err := ctx.ShouldBindJSON(&loginRequest); err != nil {
var ve validator.ValidationErrors
if errors.As(err, &ve) {
fmt.Println("validation error: ", ve)
return
}
fmt.Printf("%T\n", err)
fmt.Println("request content error: ", ve)
return
}
}
}
and when I use curl as below:
curl --location --request POST '127.0.0.1:8080/api/login' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data-raw '{
"password": "password"
}'
I get output in the terminal as follows:
validator.ValidationErrors
request content error:
despite the type of the variable loginRequest
being validator.ValidationErrors
, it couldn't be matched.