gpt4 book ai didi

http - 发送带参数的GET请求得到301状态码

转载 作者:IT王子 更新时间:2023-10-29 01:23:59 24 4
gpt4 key购买 nike

我使用 mux 设置了一个非常简单的 Go 服务器代码,当我将 curlGET 请求参数(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))
}

有人可以告诉我这个吗。谢谢

最佳答案

go doc mux.StrictSlash 指出:

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.

因此,为了避免重定向,您可以使用 mux.NewRouter().StrictSlash(false) 等同于 mux.NewRouter() 或使用带有尾部斜线即 router.HandleFunc("/suggestions/", handleSuggestions).Methods("GET")

关于http - 发送带参数的GET请求得到301状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40910008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com