简体   繁体   中英

How can I send a url-encoded url as a parameter to mux?

I have this code

func main() {
  router := mux.NewRouter()
  router.HandleFunc("/", rootHandler)
  router.HandleFunc("/xyz/{url}", urlHandler)
  http.Handle("/", router)

  log.Fatal(http.ListenAndServe(":8080", nil))
}

With this url: http://localhost:8080/xyz/https%3A%2F%2Fabc.no%2FJZ2las1o3Ct mux will redirect (301) to http://localhost:8080/xyz/https:/abc.no/JZ2las1o3Ct

If I change %2F%2F to only one ( %2F ) I do not get redirected and the escaped characters stay escaped.

I have found references to router.SkipClean(true) but it makes no difference in how Mux handles this.

What I want is that the mux variable url should hold https%3A%2F%2Fabc.no%2FJZ2las1o3Ct

What you're looking for here is the UseEncodedPath setting.

https://godoc.org/github.com/gorilla/mux#Router.UseEncodedPath

The default behaviour in mux is to decode the URL, then do the path matching.

If you turn on UseEncodedPath, it won't decode the path parameter and when you access the path param inside your handler it will still be encoded.

At that point you can choose if you want to decode the parameter or not using the go QueryUnescape function.

You can use QueryUnescape for removing the spaces and other url coded paramters. Please look for the function

https://golang.org/pkg/net/url/#QueryUnescape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM