type ValidateData struct { model.User Name string `json:"name" validate:"required"` //非空 Age uint8 `json:"age" validate:"gte=0,lte=100"` //0<=type<=100 Email string `json:"email" validate:"required,email"` //非空,email格式 } var requestData ValidateData if err := c.ShouldBindBodyWithJSON(&requestData); err != nil { errMsg := fmt.Sprintf("请求参数绑定错误: %v", err) fmt.Println(errMsg) controllers.ReturnError(c, 1001, errMsg) return } //再次绑定需要入库的字段 var insertData model.User if err := c.ShouldBindBodyWithJSON(&insertData); err != nil { errMsg := fmt.Sprintf("model参数绑定错误: %v", err) fmt.Println(errMsg) controllers.ReturnError(c, 1001, errMsg) return } fmt.Println("insertData:", insertData)
标签:body,读取,框架,json,Gin,validate From: https://www.cnblogs.com/rxbook/p/18474805