2016-12-01 14 views
2

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)) 
} 

誰かがthis.Thanksあなたがパス/suggestionsを(注:最後のスラッシュはありません)登録されたというだけの理由だ

+0

ブラウザキャッシュをクリアしてからもう一度試してください。 – Bhavana

+0

私はやりました私はカールコマンドラインを使用してイオン: –

+0

ああ..申し訳ありません..気づいていなかった.. :( – Bhavana

答えて

3

に私に光を当てることができ、あなたはURL localhost:8080/suggestions/?locale=enを呼び出し(末尾がありますスラッシュ:/suggestions)。

ルーターは、(Router.StrictSlash()ポリシーに基づいて)末尾にスラッシュが付いていない要求されたパスと一致する登録済みパスがあることを検出したため、有効な登録済みパスにつながるリダイレクトを送信します。

は単にsuggestionsの後にスラッシュを付けずにURLを使用します。

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()に相当しますmux.NewRouter().StrictSlash(false)か末尾にスラッシュが付いたURLを使用してください。つまり、router.HandleFunc("/suggestions/", handleSuggestions).Methods("GET")

+0

ありがとう、あなたの答えをupvoted :) –

関連する問題