使用Gorilla Mux如何检索可选的查询变量?

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

Retrieve optional query variables with gorilla mux?

问题

我正在编写一个处理程序,可以接受POST或GET请求。因此,我希望能够选择以下方式:

http://host/query?parm1=value&parm2=value

我原以为Gorilla mux会给我返回:

{
    "parm1": "value",
    "parm2": "value"
}

但是mux.Vars(r)是空的。我知道使用.Query("key", "value")会使参数变为必需,这不是我想要的。我漏掉了什么?

英文:

I'm writing a handler that can take either POST or GET. As such, I want the option of being able to say:

http://host/query?parm1=value&parm2=value

I was assuming that Gorilla mux would then give me:

{
    "parm1": "value",
    "parm2": "value
}

but mux.Vars(r) is empty. I'm aware that using .Query("key", "value" will make the parameters mandatory, which isn't what I want. What am I missing?

答案1

得分: 40

根据评论中的反映,基本答案是“mux不是用来做这个的”。mux非常适合解析URL的路径部分,并将其组件转换为变量。举个假设的例子,一个获取有关国家信息的调用可能有以下规范:

country/{code}

并接受以下形式的调用:

http://myhost/country/DE

你可以通过以下方式获取code参数的值:

code := mux.Vars(r)["code"]

如果你想传递查询变量,你不需要使用mux。直接从请求中获取它们即可。因此,对于以下替代的查询语法:

http://myhost/country?code=DE

你可以这样做:

code := r.URL.Query().Get("code")
英文:

As reflected in a comment, the basic answer is "that's not what mux is for". mux is great at picking apart the path portion of a URL, and turning the components into variables. To give a hypothetical example, a call that gives information about a country might have a spec that looks like this:

country/{code}

and accept calls that look like this:

http://myhost/country/DE

You'd get the value of the code parameter like this:

code := mux.Vars(r)["code"]

If you want to pass query variables, you don't do that with mux. Just grab them straight from the request. So given the alternative query syntax:

http://myhost/country?code=DE

you'd do:

code := r.URL.Query().Get("code")

答案2

得分: -2

如果你有一个像下面这样的处理函数,那么你可以使用以下两种方法之一提取查询字符串的值(这里是{param1})。

方法1:通过遍历查询字符串并打印键和值来提取。

方法2:直接使用mux Router对象提取值。

// 用于处理请求的路由器,查询字符串传递给{param1}变量
router.HandleFunc("/viewdemo/{param1}", GetDemoDetail).Methods("GET")

func GetData(response http.ResponseWriter, request *http.Request) {
    // 遍历对象并提取键和值
    for k, v := range mux.Vars(request) {
        fmt.Printf("key=%v, value=%v", k, v)
    }

    // 使用mux路由器提取查询字符串{param1}的值
    params := mux.Vars(request)
    objid := params["param1"]
    fmt.Println("查询字符串键值", objid)
}
英文:

If you have an Handler function like the following, Then you can extract the Query string value (here its {param1}) using any of the two methods.

Method 1. It iterates through the query strings and prints the key and value.

Method 2. Directly extract the value using the mux Router object.

// Rounter for handling the request, Query string passed in {param1} variable

router.HandleFunc("/viewdemo/{param1}", GetDemoDetail).Methods("GET")


func GetData(response http.ResponseWriter, request *http.Request) {
// iterate throguth the object and extract key and value
	for k, v := range mux.Vars(request) {
		fmt.Printf("key=%v, value=%v", k, v)
	}

//Use the mux router to extract the value of query string {param1}
	params := mux.Vars(request)
	objid := params["param1"]
	fmt.Println("Query string key value", objid)
}

huangapple
  • 本文由 发表于 2017年9月5日 09:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/46045756.html
匿名

发表评论

匿名网友

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

确定