当发送带参数的GET请求时,收到301状态码。

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

Get 301 status code when sending GET request with parameter

问题

我有一个非常简单的Go服务器代码设置,使用mux,当我使用curl进行GET请求参数(localhost:8080/suggestions/?locale=en)时,我得到301状态码(永久移动)。但是当没有GET参数时,它正常工作。

func main() {
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/suggestions", handleSuggestions).Methods("GET")
    log.Fatal(http.ListenAndServe("localhost:8080", router))
}

有人可以给我解释一下吗?谢谢。

英文:

I have a very simple Go server code setup with mux and when I use curl with GET request params (localhost:8080/suggestions/?locale=en), I get 301 status code (Move permanently). But when there's no get parameters, it's working just fine.

func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/suggestions", handleSuggestions).Methods("GET")
log.Fatal(http.ListenAndServe("localhost:8080", router))
}

Can somebody shed me a light on this.Thanks

答案1

得分: 4

go doc mux.StrictSlash 说明:

func (r *Router) StrictSlash(value bool) *Router

StrictSlash 方法用于定义新路由的尾部斜杠行为。初始值为 false。

当设置为 true 时,如果路由路径为 "/path/",访问 "/path" 将会重定向到前者,反之亦然。换句话说,你的应用程序将始终按照路由中指定的路径进行处理。

当设置为 false 时,如果路由路径为 "/path",访问 "/path/" 将不会匹配该路由,反之亦然。

特殊情况:当路由使用 PathPrefix() 方法设置路径前缀时,严格斜杠设置将被忽略,因为仅凭前缀无法确定重定向行为。然而,从该路由创建的任何子路由器都会继承原始的 StrictSlash 设置。

因此,为了避免重定向,你可以使用 mux.NewRouter().StrictSlash(false),它等同于 mux.NewRouter(),或者使用带有尾部斜杠的 URL,例如 router.HandleFunc("/suggestions/", handleSuggestions).Methods("GET")

英文:

go doc mux.StrictSlash states:

func (r *Router) StrictSlash(value bool) *Router
    StrictSlash defines the trailing slash behavior for new routes. The initial
    value is false.

    When true, if the route path is "/path/", accessing "/path" will redirect to
    the former and vice versa. In other words, your application will always see
    the path as specified in the route.

    When false, if the route path is "/path", accessing "/path/" will not match
    this route and vice versa.

    Special case: when a route sets a path prefix using the PathPrefix() method,
    strict slash is ignored for that route because the redirect behavior can't
    be determined from a prefix alone. However, any subrouters created from that
    route inherit the original StrictSlash setting.

So to avoid the redirects you can either mux.NewRouter().StrictSlash(false) which is equivalent to mux.NewRouter() or use a URL with a trailing slash i.e. router.HandleFunc("/suggestions/", handleSuggestions).Methods("GET")

答案2

得分: 3

这是因为你注册了路径/suggestions(注意:没有尾部斜杠),然后你调用了URLlocalhost:8080/suggestions/?locale=en/suggestions后面有一个尾部斜杠)。

你的路由器检测到有一个已注册的路径与请求的路径匹配,但没有尾部斜杠(基于你的Router.StrictSlash()策略),所以它发送了一个重定向,当跟随重定向时,会导向一个有效的已注册路径。

只需在suggestions后面使用没有尾部斜杠的URL:

localhost:8080/suggestions?locale=en
英文:

That's simply because you registered the path /suggestions (note: there is no trailing slash), and you call the URL localhost:8080/suggestions/?locale=en (there is a trailing slash after /suggestions).

You router detects that there's a registered path which would match the requested path without the trailing slash (based on your Router.StrictSlash() policy), so it sends a redirect which when followed would lead you to a valid, registered path.

Simply use a URL without trailing slash after suggestions:

localhost:8080/suggestions?locale=en

huangapple
  • 本文由 发表于 2016年12月1日 19:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/40910008.html
匿名

发表评论

匿名网友

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

确定