从Gin golang中读取QueryArray

huangapple go评论73阅读模式
英文:

Reading QueryArray from Gin golang

问题

你好,我将为你翻译以下内容:

嗨,我正在将查询参数传递给我的gin服务器,像这样:

curl -X POST
'http://localhost:4000/url?X=val1&Y=val2&x[]=1&x[]=2'

然后将其发送到我的gin处理函数中:

func handler(c *gin.Context) {
    fmt.Println(c.Query("X"))
    fmt.Println(c.Query("Y"))
    fmt.Println(c.QueryArray("x"))
}

虽然 c.Query("x")c.Query("Y") 能正常工作,但 c.QueryArray("x") 却不能正常工作!

我不确定我在这里漏掉了什么。我也尝试过使用GET请求,但也不起作用。

以下是我尝试过但没有成功的其他实验:

fmt.Println(c.Params.Get("x"))
fmt.Println(c.Params.Get("gene"))
fmt.Println(c.PostFormArray("x"))

希望对你有帮助!

英文:

Hi I am passing a query parameter to my gin server like this:

> curl -X POST \
> 'http://localhost:4000/url?X=val1&Y=val2&x[]=1&x[]=2'

This is then sent to the my gin handler function

func handler (c *gin.Context) {
  	fmt.Println(c.Query("X"))
	fmt.Println(c.Query("Y"))
	fmt.Println(c.QueryArray("x"))
}

While c.Query("x") and c.Query("Y") works, c.QueryArray("x") does not work!

I am not sure what am I missing over here. I have tried the same with GET request as well and it does not work.

Other experiments that did not work for me are here:

fmt.Println(c.Params.Get("x"))
fmt.Println(c.Params.Get("gene"))
fmt.Println(c.PostFormArray("x"))

答案1

得分: 9

从我的评论中起草的答案供 Stack Overflow 用户使用。

重复字段名和值:

curl -X POST 'http://localhost:4000/url?X=val1&Y=val2&x=1&x=2'

或者:

用逗号分隔:

curl -X POST 'http://localhost:4000/url?X=val1&Y=val2&x=1,2'
英文:

Drafted answer from my comment for SO users.

Repeat field name with values:

curl -X POST 'http://localhost:4000/url?X=val1&Y=val2&x=1&x=2'

or:

Separated by commas:

curl -X POST 'http://localhost:4000/url?X=val1&Y=val2&x=1,2'

答案2

得分: 0

这可能有点过度设计,但对我来说完成了任务:

// @Param property_type query []uint32 false "property_types"
func (svc *HttpService) acceptListofQuery(c *gin.Context) {
    var propertyTypes []uint32
    propertyTypes, done := svc.parsePropTypes(c)
    if done {
        return
    }
    // ..... 其余逻辑在这里
    c.JSON(200, "We good")
}

func (svc *HttpService) parsePropTypes(c *gin.Context) ([]uint32, bool) {
    var propertyTypes []uint32
    var propertyTypesAsString string
    var propertyTypesAsArrayOfStrings []string
    propertyTypesAsString, success := c.GetQuery("property_types")
    propertyTypesAsArrayOfStrings = strings.Split(propertyTypesAsString, ",")
    if success {
        for _, propertyTypeAsString := range propertyTypesAsArrayOfStrings {
            i, err := strconv.Atoi(propertyTypeAsString)
            if err != nil {
                svc.ErrorWithJson(c, 400, errors.New("invalid property_types array"))
                return nil, true
            }
            propertyTypes = append(propertyTypes, uint32(i))
        }
    }
    return propertyTypes, false
}

然后你可以发送类似这样的请求:

curl -X POST 'http://localhost:4000/url?property_types=1,4,7,12'
英文:

This maybe over-engineered, but it got things done for me:

// @Param property_type query []uint32 false "property_types"
func (svc *HttpService) acceptListofQuery(c *gin.Context) {
    var propertyTypes []uint32
    propertyTypes, done := svc.parsePropTypes(c)
	if done {
    	return
	}
    // ..... rest of logic here
	c.JSON(200, "We good")
}

func (svc *HttpService) parsePropTypes(c *gin.Context) ([]uint32, bool) {
    var propertyTypes []uint32
	var propertyTypesAsString string
    var propertyTypesAsArrayOfStrings []string
	propertyTypesAsString, success := c.GetQuery("property_types")
    propertyTypesAsArrayOfStrings = strings.Split(propertyTypesAsString, ",")
	if success {
    	for _, propertyTypeAsString := range propertyTypesAsArrayOfStrings {
			i, err := strconv.Atoi(propertyTypeAsString)
    		if err != nil {
	    		svc.ErrorWithJson(c, 400, errors.New("invalid property_types array"))
				return nil, true
    		}
	    	propertyTypes = append(propertyTypes, uint32(i))
		}
   }
	return propertyTypes, false
}

Then you can send something like:

curl -X POST 'http://localhost:4000/url?property_types=1,4,7,12

huangapple
  • 本文由 发表于 2017年6月13日 03:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/44507189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定