I want to send an url such as "Documents/folder1/folder2/file.txt" or it can have less slashes such as "Documents/folder1/file.txt", and I want to pass this url as a path parameter, such as router.HandleFunc("/myproject/v1/image/{url}", GetImage)
but when doing this it treats to go to the url for ex: /myproject/v1/image/Documents/folder1/file.txt and it does not find it so it returns 404.
I am using gorilla mux:
func main(){
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/myproject/v1/image/{url}", GetImage)
}
I thought that it was from the strictslash but when I set it false I remain getting 404
StrictSlashes
has to do with a single trailing slash, not with whether or not slashes are matched inside of a parameter (they aren't). You need to use PathPrefix
:
const (
imagesPrefix = "/myproject/v1/image/" // note no {url}
)
func main() {
router := mux.NewRouter()
router.PathPrefix(imagesPrefix).Handler(
http.StripPrefix(imagesPrefix, GetHandler),
)
}
func GetImage (w http.ResponseWriter, r *http.Request) {
// r.URL.Path should contain the image path.
}