Mux can't handle encoded "&" and "?" in url query params

2,926 views
Skip to first unread message

Micah Willard

unread,
Mar 23, 2017, 5:18:08 PM3/23/17
to Gorilla web toolkit
When I execute and perform a request on the following code at url: localhost:3000/?query=Business%20Management%20%26%20Administration:

package main

import (
       
"fmt"
       
"io"
       
"net/http"
       
"github.com/gorilla/mux"
       
"log"
)

func main
() {
        handler
:= func(w http.ResponseWriter, r *http.Request) {
                fmt
.Println()
                fmt
.Println(r.URL.Query())
                fmt
.Println(r.URL.Query().Get("query"))
                fmt
.Println(r.URL.RawQuery)
                fmt
.Println("Called: " + r.URL.String())
                fmt
.Println("^^^")
                fmt
.Println()
                io
.WriteString(w, "<html><body>H World!</body></html>")
       
}

        r
:= mux.NewRouter()
        r
.HandleFunc("/", handler)
        http
.Handle("/", r)
        log
.Fatal(http.ListenAndServe(":3000", r))
}

I get the following results for my prints:


map
[query:[Business Management ]  Administration:[]]
Business Management
query
=Business%20Management%20&%20Administration
Called: /?query=Business%20Management%20&%20Administration
^^^

Which is obviously wrong and i have a strong notion it is Mux. 
I created a httptest program without mux (here) and everything came out right, so that is why I am thinking something in mux might be the cause. Similar issues happen when using encoded "?" (%3F). I apologize in advance if this is something simple that I am missing. 

Thanks for your time, everyone!


Micah Willard

unread,
Mar 28, 2017, 4:46:21 PM3/28/17
to Gorilla web toolkit
Okay, This is definitely broken. LUCKILY, there is a gorilla mux way of fixing it....

You have to use the route.Queries() and give it a {name:pattern} for it to use (using {query:.+} fixes my above issue). If you give route.Queries() just {name}, it will fail with the example url I gave above. Hope this helps someone down the road.

mhh...@gmail.com

unread,
Apr 9, 2017, 5:08:04 PM4/9/17
to Gorilla web toolkit
Reply all
Reply to author
Forward
0 new messages