How to read multiple parameters from a URL

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

How to read multiple parameters from a URL

问题

我有一个反向代理API,它读取本地主机API调用的参数,然后将这些参数发送到第三方API。

如果我只使用一个参数,我可以正确地使其工作。像这样:

http://localhost:8080/path?page=1

然而,我希望能够像这样使用多个参数:

http://localhost:8080/path?page=1&param=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&amp;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()[&quot;page&quot;]

	if !ok || len(keys[0]) &lt; 1 {
		log.Println(&quot;Url Param &#39;page&#39; is missing&quot;)
		return
	}

	// Query()[&quot;key&quot;] will return an array of items,
	// we only want the single item.
	key := keys[0]

	log.Println(&quot;Url Param &#39;page&#39; is: &quot; + string(key))
	params := url.Values{
		&quot;page[size]&quot;:   []string{&quot;100&quot;},
		&quot;page[number]&quot;: []string{&quot;&quot;},
	}
	u := &amp;url.URL{
		Scheme:   &quot;https&quot;,
		Host:     &quot;url.com&quot;,
		Path:     &quot;/path&quot;,
		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[&quot;params&quot;]
keys[&quot;page&quot;]

答案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()[&quot;page&quot;]

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()[&quot;page&quot;]
keysParamA, ok := r.URL.Query()[&quot;ParamA&quot;]
keysParamB, ok := r.URL.Query()[&quot;ParamB&quot;]
keysParamC, ok := r.URL.Query()[&quot;ParamC&quot;]

Or, you can also use the r.URL.Query().Get(key) to return the param value in string type.

page := r.URL.Query().Get(&quot;page&quot;)
paramA := r.URL.Query().Get(&quot;ParamA&quot;)
paramB := r.URL.Query().Get(&quot;ParamB&quot;)
paramC := r.URL.Query().Get(&quot;ParamC&quot;)

huangapple
  • 本文由 发表于 2022年2月21日 10:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71200482.html
匿名

发表评论

匿名网友

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

确定