Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading QueryArray from Gin golang

Tags:

go

go-gin

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"))
like image 788
manugupt1 Avatar asked Apr 05 '26 16:04

manugupt1


1 Answers

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'
like image 186
jeevatkm Avatar answered Apr 08 '26 06:04

jeevatkm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!