Andy Morgan
Andy Morgan

Reputation: 93

How do I pass a URL as a parameter in a Golang route?

I'm trying to pass a URL as a parameter in Golang, and I haven't been able to find a solution in all of the tutorials I've looked at. The problem is that I can only get the url to return minus a crucial forward slash.

My handler looks like this:

router.HandleFunc("/new/{url}", createURL)

So the request would look like:

www.myapp.heroku.com/new/https://www.google.com

However, the url that I results is missing a slash:

http:/www.google.com

I sure it's probably got something to do with RFC3986, but is there a way to pass in the url as it is?

Upvotes: 2

Views: 10637

Answers (2)

Andy Morgan
Andy Morgan

Reputation: 93

This particular behavior in Gorilla Mux can be changed by setting SkipClean to true.

router := mux.NewRouter()
router.SkipClean(true)
router.HandleFunc("/new/", index)
router.HandleFunc("/", index)
http.ListenAndServe(":"+port, router)

The relevant documentation can be found here.

Upvotes: 2

putu
putu

Reputation: 6444

After reading the other question, I understand what do you mean. Implement a kind of URL re-writer before URL goes to gorilla/mux. The function will look like:

func Rewriter(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        //Simple URL rewriter. Rewrite if it's started with API path
        pathReq := r.RequestURI
        if strings.HasPrefix(pathReq, "/new/") {
            //Use url.QueryEscape for pre go1.8
            pe := url.PathEscape(strings.TrimLeft(pathReq, "/new/"))
            r.URL.Path = "/new/" + pe
            r.URL.RawQuery = ""
        }

        h.ServeHTTP(w, r)
    })
}

Wrap gorilla router when starting the http server:

r := mux.NewRouter()

// ... other handler
r.HandleFunc("/new/{original-url}", NewHandler)

//Wrap mux.Router using Rewriter
log.Fatal(http.ListenAndServe(":8080", Rewriter(r)))

Then in your URL shortener handler, the original URL can be extracted using the following code:

func NewHandler(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    ou := vars["original-url"]
    //Use url.QueryUnascape for pre go1.8
    originalURL, err := url.PathUnescape(ou)

    //... other processing
}

IMHO, implementing URL shortener service like this is not recommended, mainly due to incorrect use of HTTP verbs. Any GET request should not leave side effect in server e.g. no record creation in database, etc.

Upvotes: 2

Related Questions