jim
jim

Reputation: 111

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

Upvotes: 3

Views: 2000

Answers (2)

Lord Nikon
Lord Nikon

Reputation: 192

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.

Upvotes: 4

Himanshu
Himanshu

Reputation: 12685

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

Upvotes: -1

Related Questions