Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict Slashes don't work #472

Closed
swizzley opened this issue May 3, 2019 · 9 comments
Closed

Strict Slashes don't work #472

swizzley opened this issue May 3, 2019 · 9 comments

Comments

@swizzley
Copy link

swizzley commented May 3, 2019

What version of Go are you running? (Paste the output of go version)
go version go1.12.5 darwin/amd64

What version of gorilla/mux are you at? (Paste the output of git rev-parse HEAD inside $GOPATH/src/github.com/gorilla/mux)
github.com/gorilla/mux v1.7.2

Describe your problem (and what you have tried so far)
rtr = mux.NewRouter().StrictSlash(true)
rtr.HandleFunc("/health/", health)

opening /health will always result in 404 instead of 301

Paste a minimal, runnable, reproduction of your issue below (use backticks to format it)

package main

import (
	"github.com/gorilla/mux"
	"net/http"
	"encoding/json"
)

func main() {
	rtr := mux.NewRouter().StrictSlash(true)
	rtr.HandleFunc("/health/", health)
	s := &http.Server{
		Addr:           ":8080",
		Handler:        rtr,
	}
	s.ListenAndServe()

}

func health(w http.ResponseWriter, r *http.Request) {
	status := struct {
		Status string
	}{
		"OK",
	}
	w.Header().Set("Content-Type", "application/json")
	j, _ := json.Marshal(status)
	w.Write(j)
}
@swizzley swizzley changed the title Strict Slashes dont work Strict Slashes don't work May 3, 2019
@elithrar
Copy link
Contributor

elithrar commented May 3, 2019 via email

@mattheusv
Copy link

mattheusv commented May 9, 2019

I'm usually configure my handle func more like this:

rtr.HandleFunc("/health", health)

and, when I call /health and /health/ I get status code 200 to both

@houspiller
Copy link

I have the same problem....

router := mux.NewRouter().StrictSlash(true)
	s := router.PathPrefix("/v0").Subrouter().StrictSlash(true)

	s.HandleFunc("/health/", HealthCheckHandler).Methods("GET")
	s.HandleFunc("/tags/", CreateTagHandler).Methods("POST")
	s.HandleFunc("/tags/", GetTagsHandler).Methods("GET")
  1. The StrictSlash in subrouter dont work
  2. When I post in tags without the final '/', the return is the GET. Its bizarre...

@elithrar
Copy link
Contributor

elithrar commented May 20, 2019 via email

@houspiller
Copy link

I made a post in Postman...

[POST] http://localhost:6000/v0/tags

and the body in response is the same body for the GET

@the-jackalope
Copy link

From the docs

The re-direct is a HTTP 301 (Moved Permanently). Note that when this is set for routes with a non-idempotent method (e.g. POST, PUT), the subsequent re-directed request will be made as a GET by most clients. Use middleware or client settings to modify this behaviour as needed.

This may explain why some of you are hitting the GET handler instead of the POST when using strict slash.

@swizzley
Copy link
Author

swizzley commented Jun 19, 2019

So I updated to the latest version of Mux and the issue persists

{
			"ImportPath": "github.com/gorilla/mux",
			"Comment": "v1.7.2",
			"Rev": "ed099d42384823742bba0bf9a72b53b55c9e2e38"
},

@elithrar
Copy link
Contributor

elithrar commented Jun 19, 2019 via email

@elithrar
Copy link
Contributor

See my comments - #451 (comment) and this full example of middleware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants