这是我用于端点的主要功能的一部分
r := mux.NewRouter()
r.StrictSlash(true)
r.HandleFunc("/", test)
r.HandleFunc("/feature/list/", a.FeatureListHandler)
log.Fatal(http.ListenAndServe(":8080", r))
但是当我curl localhost:8080/feature/list
我明白了
<a hef="/feature/list">Moved Permanently</a>
然而,当我curl localhost:8080/feature/list/
我得到了我的 json。
如何才能使两条路由都返回我想要的 json。
最佳答案
从文档来看,这似乎是当 StrictSlash
为 true
时的预期行为:
http://www.gorillatoolkit.org/pkg/mux#Router.StrictSlash
也许您可以将其设置为false
,然后分别定义两个路由?
r.StrictSlash(false)
r.HandleFunc("/feature/list", a.FeatureListHandler)
r.HandleFunc("/feature/list/", a.FeatureListHandler)
关于Go Gorilla Mux 将在使用 StrictSlash(true) 时保持 301 重定向,如何返回 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46224396/