如何在Go gorilla/mux中使用可选的查询参数?

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

How to use an optional query parameter in Go gorilla/mux?

问题

可能已经有一个解决方案适用于我的问题,但我无法在任何地方找到它。我尝试了很多方法,但迄今为止都没有成功。

我有类似这样的代码:

package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
)

func HealthCheck(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	fmt.Fprintln(w, "Healthy")
	// Also print the value of 'foo'
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/health-check", HealthCheck).Methods("GET").Queries("foo", "{foo}").Name("HealthCheck")
	r.HandleFunc("/health-check", HealthCheck).Methods("GET")
	http.ListenAndServe(":8080", r)
}

我想要实现的目标是:

curl http://localhost:8080/health-check

应该返回:Healthy <foo> - > foo的默认值)

还有以下内容:

curl http://localhost:8080/health-check?foo=bar

应该返回:Healthy bar

英文:

Probably there is already a solution here to my problem, but I couldn't find it anywhere. I tried a bunch of stuff, but nothing worked so far.

I have something like this:

package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;

	&quot;github.com/gorilla/mux&quot;
)

func HealthCheck(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	fmt.Fprintln(w, &quot;Healthy&quot;)
	// Also print the value of &#39;foo&#39;
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc(&quot;/health-check&quot;, HealthCheck).Methods(&quot;GET&quot;).Queries(&quot;foo&quot;, &quot;{foo}&quot;).Name(&quot;HealthCheck&quot;)
	r.HandleFunc(&quot;/health-check&quot;, HealthCheck).Methods(&quot;GET&quot;)
	http.ListenAndServe(&quot;:8080&quot;, r)
}

What I'm trying to achieve:

curl http://localhost:8080/health-check

Should respond with: Healthy &lt;foo&gt; (<foo> -> the default value of foo)

And also the following:

curl http://localhost:8080/health-check?foo=bar

Should respond with: Healthy bar

答案1

得分: 0

一个解决方案是在处理程序中简单地处理查询参数:

package main

import (
	"net/http"

	"github.com/gorilla/mux"
)

func HealthCheckHandler(w http.ResponseWriter, req *http.Request) {
	values := req.URL.Query()
	foo := values.Get("foo")

	if foo != "" {
		w.Write([]byte("健康 " + foo))
	} else {
		w.Write([]byte("健康 <foo>"))
	}
	w.WriteHeader(http.StatusOK)
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/health-check", HealthCheckHandler)
	http.ListenAndServe(":8080", r)
}

根据gorilla/mux文档,Queries方法用于将处理程序与特定函数匹配,类似于正则表达式。

英文:

One solution if to simply handle the query params in your handler:

package main

import (
	&quot;net/http&quot;

	&quot;github.com/gorilla/mux&quot;
)

func HealthCheckHandler(w http.ResponseWriter, req *http.Request) {
	values := req.URL.Query()
	foo := values.Get(&quot;foo&quot;)

	if foo != &quot;&quot; {
		w.Write([]byte(&quot;Healthy &quot; + foo))
	} else {
		w.Write([]byte(&quot;Healthy &lt;foo&gt;&quot;))
	}
	w.WriteHeader(http.StatusOK)
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc(&quot;/health-check&quot;, HealthCheckHandler)
	http.ListenAndServe(&quot;:8080&quot;, r)
}

according to the gorilla/mux documentation, the Queries method is meant to match your handler to specific functions, akin to a regular expression.

huangapple
  • 本文由 发表于 2022年4月16日 22:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/71894537.html
匿名

发表评论

匿名网友

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

确定