Gin框架,body参数只能读取一次

 

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)

  

posted @ 2024-10-18 18:03  浮尘微光  阅读(6)  评论(0编辑  收藏  举报