2016-12-01 44 views
2
发送GET请求时获取301状态代码

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

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

有人可以摆脱我的this.Thanks光

+0

清除浏览器缓存并重试 – Bhavana

+0

我做了离子,我用卷曲命令行:) –

+0

ohh ..对不起..没有注意到.. :( – Bhavana

回答

3

那只是因为你注册的路径/suggestions(注:有没有尾随斜线),并调用URL localhost:8080/suggestions/?locale=en(有尾随在/suggestions之后削减)。

您的路由器检测到有一个注册路径与匹配所请求的路径而没有结尾斜杠(根据您的Router.StrictSlash()策略),因此它会发送一个重定向,当它遵循时会导致您注册一个有效的路径。

只需使用一个URL没有斜线suggestions后:

localhost:8080/suggestions?locale=en 
3

去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()或使用带有斜杠的URL即router.HandleFunc("/suggestions/", handleSuggestions).Methods("GET")

+0

谢谢,upvoted你的答案:) –