go:遍历获取gin请求的所有参数
背景:为了提高程序的通用性,需要将前端request中请求的参数,包含表单参数全部取到.
代码:
1 func DataMapByRequest(c *gin.Context)(dataMap map[string]string,err error) { 2 //必须先解析Form 3 c.Request.ParseForm() 4 dataMap = make(map[string]string) 5 //说明:须post方法,加: 'Content-Type': 'application/x-www-form-urlencoded' 6 for key, valueArray := range c.Request.PostForm { 7 if len(valueArray)>1{ 8 errMsg := fmt.Sprintf("#ERROR#[%s]参数设置了[%d]次,只能设置一次.",key,len(valueArray)) 9 return nil,errors.New(errMsg) 10 } 11 dataMap[key] = c.PostForm(key) 12 } 13 // 14 for key,_ := range c.Request.URL.Query(){ 15 dataMap[key] = c.Query(key) 16 } 17 18 return 19 }
本文来自博客园,作者:xiaoyongdata(微信号:xiaoyongdata),转载请注明原文链接:https://www.cnblogs.com/xiaoyongdata/p/15620735.html