How to read multiple parameters from a URL

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

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。

  1. func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
  2. // 当我尝试在列表中附加另一个查询(与page一起),我会得到一个错误
  3. keys, ok := r.URL.Query()["page"]
  4. if !ok || len(keys[0]) < 1 {
  5. log.Println("Url Param 'page' is missing")
  6. return
  7. }
  8. // Query()["key"]将返回一个项目数组,
  9. // 我们只想要单个项目。
  10. key := keys[0]
  11. log.Println("Url Param 'page' is: " + string(key))
  12. params := url.Values{
  13. "page[size]": []string{"100"},
  14. "page[number]": []string{""},
  15. }
  16. u := &url.URL{
  17. Scheme: "https",
  18. Host: "url.com",
  19. Path: "/path",
  20. RawQuery: params.Encode(),
  21. }
  22. }

在不进行重构的情况下,我是否遗漏了一些简单的东西?我如何为我的函数添加另一个参数来捕获?

英文:

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.

  1. func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
  2. // when I try to append another query in the list a long with page, I get an error
  3. keys, ok := r.URL.Query()[&quot;page&quot;]
  4. if !ok || len(keys[0]) &lt; 1 {
  5. log.Println(&quot;Url Param &#39;page&#39; is missing&quot;)
  6. return
  7. }
  8. // Query()[&quot;key&quot;] will return an array of items,
  9. // we only want the single item.
  10. key := keys[0]
  11. log.Println(&quot;Url Param &#39;page&#39; is: &quot; + string(key))
  12. params := url.Values{
  13. &quot;page[size]&quot;: []string{&quot;100&quot;},
  14. &quot;page[number]&quot;: []string{&quot;&quot;},
  15. }
  16. u := &amp;url.URL{
  17. Scheme: &quot;https&quot;,
  18. Host: &quot;url.com&quot;,
  19. Path: &quot;/path&quot;,
  20. RawQuery: params.Encode(),
  21. }
  22. }

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

你可以这样做:

  1. keys, ok := r.URL.Query()
  2. // 通过以下方式浏览 keys
  3. keys["params"]
  4. keys["page"]
英文:

r.URL.Query() returns a map[string][]string

you can do a

  1. keys, ok := r.URL.Query()
  2. //browse through keys by
  3. keys[&quot;params&quot;]
  4. keys[&quot;page&quot;]

答案2

得分: 1

下面的代码行...

  1. keys, ok := r.URL.Query()["page"]

它返回page参数的值,但是以[]string类型返回。要检索更多的参数,只需添加类似的语句,但使用不同的参数名称。例如:

  1. keysPage, ok := r.URL.Query()["page"]
  2. keysParamA, ok := r.URL.Query()["ParamA"]
  3. keysParamB, ok := r.URL.Query()["ParamB"]
  4. keysParamC, ok := r.URL.Query()["ParamC"]

或者,你也可以使用r.URL.Query().Get(key)string类型返回参数值。

  1. page := r.URL.Query().Get("page")
  2. paramA := r.URL.Query().Get("ParamA")
  3. paramB := r.URL.Query().Get("ParamB")
  4. paramC := r.URL.Query().Get("ParamC")
英文:

The line of code below ...

  1. 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:

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

  1. page := r.URL.Query().Get(&quot;page&quot;)
  2. paramA := r.URL.Query().Get(&quot;ParamA&quot;)
  3. paramB := r.URL.Query().Get(&quot;ParamB&quot;)
  4. 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:

确定