将数组/切片作为URL参数传递

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

Passing an array/slice as a url parameter

问题

我有一个简单的Web服务器,拦截地理空间地图瓦片请求,替换像素,并将图像传递给前端进行服务。它运行得很好,但请求变得相当大。我想知道是否可以传递一个数组或切片来代替?我在搜索中找不到相关信息。例如:

http://localhost:8002/tiles?url=url&r=0&g=250&b=0&a=230&replaceR=0&replaceG=127&replaceB=0&replaceA=0

是我典型的请求。我想要添加更多的颜色进行替换,所以如果我可以传递类似这样的内容就太好了:

http://localhost:8002/tiles?url=url&rgba1=[0,250,0,230,0,127,0,0]&rgba2=[250,0,100,100,200,0,20,180]

这种方式可行吗?我尝试使用循环来实现:

form := make(map[string][]uint8)
for k, v := range r.URL.Query() {
    arr := []uint8{}
    for i := 0; i < len(v); i++ {
        val, err := strconv.ParseInt(v[i], 10, 32)
        arr = append(arr, uint8(val))
        if err != nil {
        }
    }
    form[k] = arr
}

但它只打印出 [0]。

英文:

I have a simple web server that intercepts geospatial map tile requests, swaps out pixels, and passes the image along to a front end to serve them. It works great, but the request is getting quite large. I was wondering if I could pass an array or slice instead? I couldn't seem to find anything searching. For example:

http://localhost:8002/tiles?url=url&amp;r=0&amp;g=250&amp;b=0&amp;a=230&amp;replaceR=0&amp;replaceG=127&amp;replaceB=0&amp;replaceA=0

is my typical request. I want to add more colors to swap so it would be great if I could pass something like:

http://localhost:8002/tiles?url=url&amp;rgba1=[0,250,0,230,0,127,0,0]&amp;rgba2=[250,0,100,100,200,0,20,180]

Is this possible? I tried to loop it like:

form := make(map[string][]uint8)
for k, v := range r.URL.Query() {
	arr := []uint8{}
	for i := 0; i &lt; len(v); i++ {
		val, err := strconv.ParseInt(v[i], 10, 32)
		arr = append(arr, uint8(val))
		if err != nil {
		}
	}
	form[k] = arr
}

But it just prints [0]

答案1

得分: 5

你的GET请求中的参数不是数组。

rgba1=0&amp;rgba1=250&amp;rgba1=0&amp;rgba1=230&amp;rgba1=0&amp;rgba1=127&amp;rgba1=0&amp;rgba1=0

将其转换为数组形式:rgba1=[0,250,0,230,0,127,0,0]

参考链接:https://golang.org/pkg/net/url/#Values

英文:

Your parameters in GET request aren't arrays.

rgba1=0&amp;rgba1=250&amp;rgba1=0&amp;rgba1=230&amp;rgba1=0&amp;rgba1=127&amp;rgba1=0&amp;rgba1=0

Creates an array rgba1=[0,250,0,230,0,127,0,0]

See https://golang.org/pkg/net/url/#Values

huangapple
  • 本文由 发表于 2016年3月10日 22:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/35919957.html
匿名

发表评论

匿名网友

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

确定