英文:
How to read multiple parameters from a URL
问题
我有一个反向代理API,它读取本地主机API调用的参数,然后将这些参数发送到第三方API。
如果我只使用一个参数,我可以正确地使其工作。像这样:
http://localhost:8080/path?page=1
然而,我希望能够像这样使用多个参数:
http://localhost:8080/path?page=1¶m=x
请参考下面的代码:
这个函数捕获一个HTTP请求,然后将这些参数发送到另一个API。
func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
// 当我尝试在列表中附加另一个查询(与page一起),我会得到一个错误
keys, ok := r.URL.Query()["page"]
if !ok || len(keys[0]) < 1 {
log.Println("Url Param 'page' is missing")
return
}
// Query()["key"]将返回一个项目数组,
// 我们只想要单个项目。
key := keys[0]
log.Println("Url Param 'page' is: " + string(key))
params := url.Values{
"page[size]": []string{"100"},
"page[number]": []string{""},
}
u := &url.URL{
Scheme: "https",
Host: "url.com",
Path: "/path",
RawQuery: params.Encode(),
}
}
在不进行重构的情况下,我是否遗漏了一些简单的东西?我如何为我的函数添加另一个参数来捕获?
英文:
I have a reverse proxy API that reads the parameters of a localhost API call and then sends those parameters to a 3rd party API.
I'm able to get this working correctly if I only use one parameter. Like so:
http://localhost:8080/path?page=1
I want to be able to use more than one parameter however like so:
http://localhost:8080/path?page=1&param=x
Please see my code below:
This function catches an HTTP request and then sends those parameters to another API.
func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
// when I try to append another query in the list a long with page, I get an error
keys, ok := r.URL.Query()["page"]
if !ok || len(keys[0]) < 1 {
log.Println("Url Param 'page' is missing")
return
}
// Query()["key"] will return an array of items,
// we only want the single item.
key := keys[0]
log.Println("Url Param 'page' is: " + string(key))
params := url.Values{
"page[size]": []string{"100"},
"page[number]": []string{""},
}
u := &url.URL{
Scheme: "https",
Host: "url.com",
Path: "/path",
RawQuery: params.Encode(),
}
}
Without having to refractor, am I missing something simple here? How can I add another parameter for my function to catch?
答案1
得分: 1
r.URL.Query()
返回一个 map[string][]string
。
你可以这样做:
keys, ok := r.URL.Query()
// 通过以下方式浏览 keys
keys["params"]
keys["page"]
英文:
r.URL.Query()
returns a map[string][]string
you can do a
keys, ok := r.URL.Query()
//browse through keys by
keys["params"]
keys["page"]
答案2
得分: 1
下面的代码行...
keys, ok := r.URL.Query()["page"]
它返回page
参数的值,但是以[]string
类型返回。要检索更多的参数,只需添加类似的语句,但使用不同的参数名称。例如:
keysPage, ok := r.URL.Query()["page"]
keysParamA, ok := r.URL.Query()["ParamA"]
keysParamB, ok := r.URL.Query()["ParamB"]
keysParamC, ok := r.URL.Query()["ParamC"]
或者,你也可以使用r.URL.Query().Get(key)
以string
类型返回参数值。
page := r.URL.Query().Get("page")
paramA := r.URL.Query().Get("ParamA")
paramB := r.URL.Query().Get("ParamB")
paramC := r.URL.Query().Get("ParamC")
英文:
The line of code below ...
keys, ok := r.URL.Query()["page"]
it returns the param value of page
, but in []string
type. To retrieve more params, simply add similar statement with different param name. for example:
keysPage, ok := r.URL.Query()["page"]
keysParamA, ok := r.URL.Query()["ParamA"]
keysParamB, ok := r.URL.Query()["ParamB"]
keysParamC, ok := r.URL.Query()["ParamC"]
Or, you can also use the r.URL.Query().Get(key)
to return the param value in string
type.
page := r.URL.Query().Get("page")
paramA := r.URL.Query().Get("ParamA")
paramB := r.URL.Query().Get("ParamB")
paramC := r.URL.Query().Get("ParamC")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论